package com.fau.cm.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Hibernate;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.fau.cm.action.index.MedicalAlternately;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCall;
import com.fau.cm.entity.MedicalCallCaller;
import com.fau.cm.entity.MedicalCallCustomer;
import com.fau.cm.entity.MedicalCallEnd;
import com.fau.cm.entity.MedicalCallExtension;
import com.fau.cm.entity.MedicalEnterpriseOutside;
import com.fau.cm.entity.MedicalKeyValue;
import com.fau.cm.entity.MedicalCallPending;
import com.fau.cm.entity.MedicalCallTelInfo;
import com.fau.cm.entity.MedicalCallTerminate;
import com.fau.cm.entity.MedicalCallUntreated;
import com.fau.cm.entity.MedicalGroup;
import com.fau.cm.entity.MedicalMediaPlacement;
import com.fau.cm.entity.MedicalPlatform;
import com.fau.cm.service.ICallManagerService;
import com.fau.cm.util.DateUtil;
import com.fau.cm.util.ExcelUtil;
import com.fau.cm.util.HttpRequestUtil;
import com.fau.cm.util.Page;
import com.fau.cm.util.ParamUtil;
import com.fau.cm.util.PhoneNumUtil;
import com.fau.cm.util.SerialNumUtil;
import com.fau.cm.util.SystemConfig;
import com.fau.cm.util.ValidatorUtil;

@Service
@Transactional(readOnly = true)
public class CallManagerServiceImpl extends BaseService implements
		ICallManagerService {
	
	@Resource
	private HttpRequestUtil httpRequestUtil;

	@SuppressWarnings("unchecked")
	@Override
	public Page findCallByPage(MedicalCall call, MedicalAdmin admin,
			Map<String, Object> map) {
		StringBuilder statusSql = new StringBuilder("from MedicalCall a where a.hospitalId=? ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCall a where a.hospitalId=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		
		if(SystemConfig.CALL_TASK_LIST_UNALLOCATE.equals(call.getListType())) {
			statusSql.append("and a.grade=? ");
			countSql.append("and a.grade=? ");
		}else {
			statusSql.append("and a.grade <> ? ");
			countSql.append("and a.grade <> ? ");
		}
		objectList.add(SystemConfig.CALL_TASK_GRADE_INIT);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			countSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			countSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			countSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			countSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			countSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			countSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			countSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			countSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by a.createTime desc ");
		}
		
		Page p = this.iBaseDao.getPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20)
				, map, countSql.toString(), statusSql.toString(), objectList);
		List<MedicalCall> list = (List<MedicalCall>) p.getResult();
		for (MedicalCall medicalCall : list) {
			if(StringUtils.isNotBlank(medicalCall.getCreateUser())) {
				MedicalAdmin ma = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
				medicalCall.setCreateUserName(ma.getName());
			}
			MedicalKeyValue category = this.iBaseDao.getValueByKey(admin.getEnterpriseId(), medicalCall.getCategory());
			if(null != category){
				medicalCall.setCategoryValue(category.getKeyValue());
			}
		}
		return p;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Page findCallUntreatedByPage(MedicalCallUntreated call,
			MedicalAdmin admin, Map<String, Object> map) {
		
		StringBuilder statusSql = new StringBuilder("from MedicalCallUntreated a where 1=1 and a.hospitalId=? and a.grade <> ? and a.transferUser=? ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallUntreated a where 1=1 and a.hospitalId=? and a.grade <> ? and a.transferUser=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		objectList.add(SystemConfig.CALL_TASK_GRADE_INIT);
		objectList.add(SystemConfig.CALL_UNT_NO_TRANSFER);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			countSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			countSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			countSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			countSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			countSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			countSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			countSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			countSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			countSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		//排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by a.createTime desc");
		}
		
		Page p = this.iBaseDao.getPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20)
				, map, countSql.toString(), statusSql.toString(), objectList);
		List<MedicalCallUntreated> list = (List<MedicalCallUntreated>) p.getResult();
		for (MedicalCallUntreated medicalCallUntreated : list) {
			if(StringUtils.isNotBlank(medicalCallUntreated.getCreateUser())) {
				MedicalAdmin ma = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, medicalCallUntreated.getCreateUser());
				medicalCallUntreated.setCreateUserName(ma.getName());
			}
			if(StringUtils.isNotBlank(medicalCallUntreated.getHandleUserId())) {
				MedicalAdmin handleUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, medicalCallUntreated.getHandleUserId());
				medicalCallUntreated.setHandleUserName(handleUser.getName());
			}
			MedicalKeyValue category = this.iBaseDao.getValueByKey(admin.getEnterpriseId(), medicalCallUntreated.getCategory());
			if(null != category){
				medicalCallUntreated.setCategoryValue(category.getKeyValue());
			}
		}
		return p;
	}

	@Override
	public Page findCallPendingByPage(MedicalCallPending call,
			MedicalAdmin admin, Map<String, Object> map) {
		
		StringBuilder statusSql = new StringBuilder("from MedicalCallPending a where 1=1 and a.hospitalId=? and a.grade <> ? ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallPending a where 1=1 and a.hospitalId=? and a.grade <> ? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		objectList.add(SystemConfig.CALL_TASK_GRADE_INIT);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			countSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			countSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			countSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			countSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			countSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			countSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			countSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			countSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			countSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		 
		//排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by a.createTime desc");
		}
		
		Page p = this.iBaseDao.getPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20)
				, map, countSql.toString(), statusSql.toString(), objectList);
		@SuppressWarnings("unchecked")
		List<MedicalCallPending> list = (List<MedicalCallPending>) p.getResult();
		for (MedicalCallPending medicalCallPending : list) {
			if(StringUtils.isNotBlank(medicalCallPending.getCreateUser())) {
				MedicalAdmin ma = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, medicalCallPending.getCreateUser());
				medicalCallPending.setCreateUserName(ma.getName());
			}
			if(StringUtils.isNotBlank(medicalCallPending.getHandleUserId())) {
				MedicalAdmin handleUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, medicalCallPending.getHandleUserId());
				medicalCallPending.setHandleUserName(handleUser.getName());
			}
			MedicalKeyValue category = this.iBaseDao.getValueByKey(admin.getEnterpriseId(), medicalCallPending.getCategory());
			if(null != category){
				medicalCallPending.setCategoryValue(category.getKeyValue());
			}
		}
		return p;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Page findCallTransferByPage(MedicalCallUntreated call,
			MedicalAdmin admin, Map<String, Object> map) {
		
		StringBuilder statusSql = new StringBuilder("from MedicalCallUntreated a where 1=1 and a.hospitalId=? and a.grade <> ? and a.transferUser <> ? ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallUntreated a where 1=1 and a.hospitalId=? and a.grade <> ? and a.transferUser <> ? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		objectList.add(SystemConfig.CALL_TASK_GRADE_INIT);
		objectList.add(SystemConfig.CALL_UNT_NO_TRANSFER);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			countSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			countSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			countSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			countSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			countSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			countSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			countSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			countSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			countSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by a.createTime desc ");
		}
		
		Page p = this.iBaseDao.getPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20)
				, map, countSql.toString(), statusSql.toString(), objectList);
		
		List<MedicalCallUntreated> list = (List<MedicalCallUntreated>) p.getResult();
		for (MedicalCallUntreated bean : list) {
			if(StringUtils.isNotBlank(bean.getCreateUser())) {
				MedicalAdmin ma = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getCreateUser());
				bean.setCreateUserName(ma.getName());
			}
			MedicalAdmin transferUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getTransferUser());
			if(null != transferUser) {
				bean.setTransferUserName(transferUser.getName());
			}
			if(StringUtils.isNotBlank(bean.getHandleUserId())) {
				MedicalAdmin handleUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getHandleUserId());
				bean.setHandleUserName(handleUser.getName());
			}
			MedicalKeyValue category = this.iBaseDao.getValueByKey(admin.getEnterpriseId(), bean.getCategory());
			if(null != category){
				bean.setCategoryValue(category.getKeyValue());
			}
		}
		return p;
	}

	@Override
	public Page findCallEndByPage(MedicalCallEnd call, MedicalAdmin admin,
			Map<String, Object> map) {
		
		StringBuilder statusSql = new StringBuilder("from MedicalCallEnd a where 1=1 and a.hospitalId=? and a.grade <> ? ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallEnd a where 1=1 and a.hospitalId=? and a.grade <> ? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		objectList.add(SystemConfig.CALL_TASK_GRADE_INIT);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			countSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			countSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			countSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			countSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			countSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			countSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			countSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			countSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			countSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by a.createTime desc ");
		}
				
		Page p = this.iBaseDao.getPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20)
				, map, countSql.toString(), statusSql.toString(), objectList);
		@SuppressWarnings("unchecked")
		List<MedicalCallEnd> list = (List<MedicalCallEnd>) p.getResult();
		for (MedicalCallEnd medicalCallEnd : list) {
			if(StringUtils.isNotBlank(medicalCallEnd.getCreateUser())) {
				MedicalAdmin ma = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, medicalCallEnd.getCreateUser());
				medicalCallEnd.setCreateUserName(ma.getName());
			}
			if(StringUtils.isNotBlank(medicalCallEnd.getHandleUserId())) {
				MedicalAdmin handleUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, medicalCallEnd.getHandleUserId());
				medicalCallEnd.setHandleUserName(handleUser.getName());
			}
			MedicalKeyValue category = this.iBaseDao.getValueByKey(admin.getEnterpriseId(), medicalCallEnd.getCategory());
			if(null != category){
				medicalCallEnd.setCategoryValue(category.getKeyValue());
			}
		}
		return p;
	}
	
	@Override
	public Page findCallTerminateByPage(MedicalCallTerminate call, MedicalAdmin admin,
			Map<String, Object> map) {
		
		StringBuilder statusSql = new StringBuilder("from MedicalCallTerminate a where 1=1 and a.hospitalId=? and a.grade <> ? ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallTerminate a where 1=1 and a.hospitalId=? and a.grade <> ? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		objectList.add(SystemConfig.CALL_TASK_GRADE_INIT);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			countSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			countSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			countSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			countSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			countSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			countSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			countSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			countSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			countSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by a.createTime desc ");
		}
		
		Page p = this.iBaseDao.getPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20)
				, map, countSql.toString(), statusSql.toString(), objectList);
		@SuppressWarnings("unchecked")
		List<MedicalCallTerminate> list = (List<MedicalCallTerminate>) p.getResult();
		for (MedicalCallTerminate medicalCallTerminate : list) {
			if(StringUtils.isNotBlank(medicalCallTerminate.getCreateUser())) {
				MedicalAdmin ma = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, medicalCallTerminate.getCreateUser());
				medicalCallTerminate.setCreateUserName(ma.getName());
			}
			MedicalKeyValue category = this.iBaseDao.getValueByKey(admin.getEnterpriseId(), medicalCallTerminate.getCategory());
			if(null != category){
				medicalCallTerminate.setCategoryValue(category.getKeyValue());
			}
		}
		return p;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean callAllocate(String callsId, String usersId) {
		String callId[] = callsId.split(",");
		String userId[] = usersId.split(",");
		List<String> callIdList = new ArrayList<String>(Arrays.asList(callId));
		List<String> userIdList = new ArrayList<String>(Arrays.asList(userId));
		
		boolean flag = this.isAllocation(callId);
		if(flag) {
			for(int i=0; i<callId.length; i++){
				MedicalCall call = (MedicalCall) this.iBaseDao.getObject(MedicalCall.class, callIdList.get(i));
				MedicalAdmin handleUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, userIdList.get(i % userIdList.size()));
				call.setCenterId(handleUser.getGroup().getPlatform().getId());
				call.setGroupId(handleUser.getGroup().getId());
				call.setHandleUserId(handleUser.getId());
				call.setGrade(SystemConfig.CALL_TASK_GRADE_USER);
				call.setAllocationTime(new Date());
				MedicalCallUntreated callUntreated = new MedicalCallUntreated();
				BeanUtils.copyProperties(call, callUntreated);
				callUntreated.setTransferUser(SystemConfig.CALL_UNT_NO_TRANSFER);
				callUntreated.setUpdateTime(new Date());
				this.iBaseDao.addObject(callUntreated);
				this.iBaseDao.deleteObject(call);
				
				// 将号码添加到中间表，二次进线找人时需要。
				List<MedicalCallTelInfo> telInfoList = this.iBaseDao.getBySql("from MedicalCallTelInfo a where a.callId=? ", null, callUntreated.getId());
				List<MedicalEnterpriseOutside> eOutsideList = this.iBaseDao.getBySql("from MedicalEnterpriseOutside a where a.eid=? ", null, callUntreated.getHospitalId());
				for(MedicalCallTelInfo telInfo : telInfoList) {
					for(MedicalEnterpriseOutside eOutside : eOutsideList) {
						MedicalCallExtension callExtension = new MedicalCallExtension();
						callExtension.setAttendantNo(handleUser.getUsername());
						callExtension.setCustomerNum(telInfo.getPhoneNum() + "," + eOutside.getOutSideNum());
						this.iBaseDao.saveObject(callExtension);
					}
				}
			}
			
			// 发送提示消息给任务的接受者
			if(callIdList.size() > userIdList.size()) {
				for(String uId : userIdList) {
					MedicalAdmin admin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, uId);
					httpRequestUtil.allocateRemind("|您有新的话务任务!", admin.getEnterpriseId(), admin.getId());
				}
			} else {
				for(int i=0; i<callId.length; i++) {
					MedicalAdmin handleUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, userIdList.get(i % userIdList.size()));
					httpRequestUtil.allocateRemind("|您有新的话务任务!", handleUser.getEnterpriseId(), handleUser.getId());
				}
			}
			
			return true;
		} else {
			return false;
		}
		
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean callAllocateCenter(String callsId, String centersId) {
		String callId[] = callsId.split(",");
		String centerId[] = centersId.split(",");
		List<String> callIdList = new ArrayList<String>(Arrays.asList(callId));
		List<String> centerIdList = new ArrayList<String>(Arrays.asList(centerId));
		
		boolean flag = this.isAllocation(callId);
		if(flag) {
			for(int i=0; i<callId.length; i++){
				MedicalCall call = (MedicalCall) this.iBaseDao.getObject(MedicalCall.class, callIdList.get(i));
				call.setCenterId(centerIdList.get(i % centerIdList.size()));
				call.setGrade(SystemConfig.CALL_TASK_GRADE_CENTER);
				call.setAllocationTime(new Date());
			}
			return flag;
		} else {
			return false;
		}
	}

	/**
	 * @desc 判断任务是否已经分配
	 * @param callId
	 * @return boolean
	 * @author dangjh
	 * 2015年2月7日 下午3:56:13
	 */
	private boolean isAllocation(String callId[]) {
		for(String id : callId) {
			MedicalCall call = (MedicalCall) this.iBaseDao.getObject(MedicalCall.class, id);
			if(null == call) return false;
			if(!SystemConfig.CALL_TASK_GRADE_INIT.equals(call.getGrade())) {
				return false;
			}
		}
		return true;
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean callAllocateGroup(String callsId, String groupsId) {
		String callId[] = callsId.split(",");
		String groupId[] = groupsId.split(",");
		List<String> callIdList = new ArrayList<String>(Arrays.asList(callId));
		List<String> groupIdList = new ArrayList<String>(Arrays.asList(groupId));
		
		boolean flag = this.isAllocation(callId);
		if(flag) {
			for(int i=0; i<callId.length; i++){
				MedicalCall call = (MedicalCall) this.iBaseDao.getObject(MedicalCall.class, callIdList.get(i));
				MedicalGroup group = (MedicalGroup) this.iBaseDao.getObject(MedicalGroup.class, groupIdList.get(i % groupIdList.size()));
				call.setCenterId(group.getPlatform().getId());
				call.setGroupId(group.getId());
				call.setGrade(SystemConfig.CALL_TASK_GRADE_GROUP);
				call.setAllocationTime(new Date());
			}
			return flag;
		} else {
			return false;
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delCall(MedicalCall call) {
		if(call.getId().indexOf(",") != -1){
			String[] callIds = call.getId().split(",");
			for(String callId : callIds){
				this.iBaseDao.updateHql("delete from MedicalCall a where a.id=? ", callId);
				this.iBaseDao.updateHql("delete from MedicalCallTelInfo a where a.callId=? ", callId);
			}
		}else{
			this.iBaseDao.updateHql("delete from MedicalCallTelInfo a where a.callId=? ", call.getId());
			this.iBaseDao.deleteObject(call);
		}
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delUntreatedCall(MedicalCallUntreated callUntreated) {
		if(callUntreated.getId().indexOf(",") != -1){
			String[] callIds = callUntreated.getId().split(",");
			for(String callId : callIds){
				this.iBaseDao.updateHql("delete from MedicalCallUntreated a where a.id=? ", callId);
				this.iBaseDao.updateHql("delete from MedicalCallTelInfo a where a.callId=? ", callId);
			}
		}else{
			this.iBaseDao.updateHql("delete from MedicalCallTelInfo a where a.callId=? ", callUntreated.getId());
			this.iBaseDao.deleteObject(callUntreated);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delPendingCall(MedicalCallPending callPending) {
		if(callPending.getId().indexOf(",") != -1){
			String[] callIds = callPending.getId().split(",");
			for(String callId : callIds){
				this.iBaseDao.updateHql("delete from MedicalCallPending a where a.id=? ", callId);
				this.iBaseDao.updateHql("delete from MedicalCallTelInfo a where a.callId=? ", callId);
			}
		}else{
			this.iBaseDao.updateHql("delete from MedicalCallTelInfo a where a.callId=? ", callPending.getId());
			this.iBaseDao.deleteObject(callPending);
		}		
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delEndCall(MedicalCallEnd callEnd) {
		if(callEnd.getId().indexOf(",") != -1){
			String[] callIds = callEnd.getId().split(",");
			for(String callId : callIds){
				this.iBaseDao.updateHql("delete from MedicalCallEnd a where a.id=? ", callId);
				this.iBaseDao.updateHql("delete from MedicalCallTelInfo a where a.callId=? ", callId);
			}
		}else{
			this.iBaseDao.updateHql("delete from MedicalCallTelInfo a where a.callId=? ", callEnd.getId());
			this.iBaseDao.deleteObject(callEnd);
		}
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delTerminateCall(MedicalCallTerminate callTerminate) {
		if(callTerminate.getId().indexOf(",") != -1){
			String[] callIds = callTerminate.getId().split(",");
			for(String callId : callIds){
				this.iBaseDao.updateHql("delete from MedicalCallTerminate a where a.id=? ", callId);
				this.iBaseDao.updateHql("delete from MedicalCallTelInfo a where a.callId=? ", callId);
			}
		}else{
			this.iBaseDao.deleteObject(callTerminate);
			this.iBaseDao.updateHql("delete from MedicalCallTelInfo a where a.callId=? ", callTerminate.getId());
		}
	}

	@Override
	public boolean validPhoneNum(MedicalAdmin admin, String phoneNum) {
		MedicalCallTelInfo telInfo = (MedicalCallTelInfo) this.iBaseDao.getObjectByHql("from MedicalCallTelInfo a where a.eid=? and a.phoneNum=? ", admin.getEnterpriseId(), phoneNum);
		if(null != telInfo) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean saveCall(MedicalCall call, MedicalAdmin admin) {
		String uid = UUID.randomUUID().toString();
		String id = uid.replace("-", "");
		call.setSerialNum(SerialNumUtil.code());
		call.setId(id);
		call.setCreateUser(admin.getId());
		call.setHospitalId(admin.getEnterpriseId());
		call.setGrade(SystemConfig.CALL_TASK_GRADE_INIT);
		call.setIncomingCount(0);
		Set<ConstraintViolation<Object>> errorInfo = ValidatorUtil.validateObj(call);
		if(errorInfo.size() > 0) {
			return false;
		}else {
			this.iBaseDao.addObject(call);
			MedicalCallTelInfo callTelInfo = new MedicalCallTelInfo();
			if(SystemConfig.CUSTOMER_TEL_PHONE_TYPE.equals(call.getNumType())) {
				callTelInfo.setType(SystemConfig.CUSTOMER_TEL_PHONE_TYPE);
			} else {
				callTelInfo.setType(SystemConfig.CUSTOMER_TEL_FIXED_TYPE);
			}
			callTelInfo.setIsMain(SystemConfig.CUSTOMER_TEL_IS_MAIN);
			callTelInfo.setPhoneNum(call.getRelatedPhone());
			callTelInfo.setCallId(call.getId());
			callTelInfo.setEid(admin.getEnterpriseId());
			this.iBaseDao.saveObject(callTelInfo);
			return true;
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void importSave(MedicalAdmin admin, File file) throws Exception {
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		Iterator<Row> rows = sheet.rowIterator();
		Row row;
		// 循环导入
		while(rows.hasNext()){
			row = rows.next();
			if(row.getRowNum() == 0) continue;
			MedicalCall call = new MedicalCall();
			call.setSerialNum(SerialNumUtil.code());
			String relatedPhone = null;
			if(HSSFCell.CELL_TYPE_STRING == row.getCell(0).getCellType()) {
				relatedPhone = row.getCell(0).getStringCellValue().trim();
			} else {
				relatedPhone = String.format("%.0f",row.getCell(0).getNumericCellValue());
			}
			
			MedicalCallTelInfo telInfo = (MedicalCallTelInfo) this.iBaseDao.getObjectByHql("from MedicalCallTelInfo a where a.phoneNum=? ", relatedPhone);
			if(null == telInfo) {
				call.setRelatedPhone(relatedPhone);
				MedicalKeyValue category = (MedicalKeyValue) this.iBaseDao.getObjectByHql("from MedicalKeyValue a where a.keyValue=? ", row.getCell(1).getStringCellValue());
				call.setCategory(category.getKeyName());
				
				if(null != row.getCell(2)) {
					call.setRemark(row.getCell(2).getStringCellValue());
				}
				call.setCreateUser(admin.getId());
				call.setHospitalId(admin.getEnterpriseId());
				call.setGrade(SystemConfig.CALL_TASK_GRADE_INIT);
				String uid = UUID.randomUUID().toString();
				String id = uid.replace("-", "");
				call.setId(id);
				call.setIncomingCount(0);
				this.iBaseDao.addObject(call);
				
				telInfo = new MedicalCallTelInfo();
				telInfo.setCallId(call.getId());
				telInfo.setPhoneNum(relatedPhone);
				telInfo.setIsMain(SystemConfig.CUSTOMER_TEL_IS_MAIN);
				telInfo.setEid(admin.getEnterpriseId());
				this.iBaseDao.saveObject(telInfo);
			}
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalPlatform> findPlatformList(MedicalAdmin admin) {
		return this.iBaseDao.getBySql("from MedicalPlatform a where a.eid=? and a.platformType=? ", null, admin.getEnterpriseId(), SystemConfig.CALL_CENTER_TYPE);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalGroup> findGroupList(String platformId) {
		return this.iBaseDao.getBySql("from MedicalGroup a where a.platform.id=? ", null, platformId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalAdmin> findUserList(String groupId) {
		return this.iBaseDao.getBySql("from MedicalAdmin a where a.group.id=? and a.onlineStatus=?", null, groupId, SystemConfig.ONLINE_STATUS);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallCategory(String callIds, String categoryKey) {
		String callId[] = callIds.split(",");
		for(String cId : callId){
			MedicalCall call = (MedicalCall) this.iBaseDao.getObject(MedicalCall.class, cId);
			call.setCategory(categoryKey);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallUntreatedCategory(String callIds, String categoryKey) {
		String callId[] = callIds.split(",");
		for(String cId : callId){
			MedicalCallUntreated callUntreated = (MedicalCallUntreated) this.iBaseDao.getObject(MedicalCallUntreated.class, cId);
			callUntreated.setCategory(categoryKey);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallPendingCategory(String callIds, String categoryKey) {
		String callId[] = callIds.split(",");
		for(String cId : callId){
			MedicalCallPending callPending = (MedicalCallPending) this.iBaseDao.getObject(MedicalCallPending.class, cId);
			callPending.setCategory(categoryKey);
		}		
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallEndCategory(String callIds, String categoryKey) {
		String callId[] = callIds.split(",");
		for(String cId : callId){
			MedicalCallEnd callEnd = (MedicalCallEnd) this.iBaseDao.getObject(MedicalCallEnd.class, cId);
			callEnd.setCategory(categoryKey);
		}
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallTerminateCategory(String callIds, String categoryKey) {
		String callId[] = callIds.split(",");
		for(String cId : callId){
			MedicalCallTerminate callTerminate = (MedicalCallTerminate) this.iBaseDao.getObject(MedicalCallTerminate.class, cId);
			callTerminate.setCategory(categoryKey);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallStatus(String callIds, String statusKey) {
		String callId[] = callIds.split(",");
		MedicalCall call = null;
		for(String cId : callId) {
			call = (MedicalCall) this.iBaseDao.getObject(MedicalCall.class, cId);
			if(SystemConfig.CALL_TASK_UNTREATED.equals(statusKey)) {
				MedicalCallUntreated callUntreated = new MedicalCallUntreated();
				BeanUtils.copyProperties(call, callUntreated);
				callUntreated.setTransferUser(SystemConfig.CALL_UNT_NO_TRANSFER);
				this.iBaseDao.deleteObject(call);
				this.iBaseDao.addObject(callUntreated);
			}else if(SystemConfig.CALL_TASK_PENDING.equals(statusKey)) {
				MedicalCallPending callPending = new MedicalCallPending();
				BeanUtils.copyProperties(call, callPending);
				this.iBaseDao.deleteObject(call);
				this.iBaseDao.addObject(callPending);				
			}else if(SystemConfig.CALL_TASK_TRANSFER.equals(statusKey)) {
				MedicalCallUntreated callUntreated = new MedicalCallUntreated();
				BeanUtils.copyProperties(call, callUntreated);
				callUntreated.setTransferUser("");
				this.iBaseDao.deleteObject(call);
				this.iBaseDao.addObject(callUntreated);
			}else if(SystemConfig.CALL_TASK_END.equals(statusKey)) {
				MedicalCallEnd callEnd = new MedicalCallEnd();
				BeanUtils.copyProperties(call, callEnd);
				this.iBaseDao.deleteObject(call);
				this.iBaseDao.addObject(callEnd);
			}else if(SystemConfig.CALL_TASK_TERMINATE.equals(statusKey)) {
				MedicalCallTerminate callTerminate = new MedicalCallTerminate();
				BeanUtils.copyProperties(call, callTerminate);
				this.iBaseDao.deleteObject(call);
				this.iBaseDao.addObject(callTerminate);
			}else {
				break;
			}
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallUntreatedSta(String callIds, String statusKey) {
		String callId[] = callIds.split(",");
		MedicalCallUntreated callUntreated = null;
		for(String cId : callId) {
			callUntreated = (MedicalCallUntreated) this.iBaseDao.getObject(MedicalCallUntreated.class, cId);
			if(SystemConfig.CALL_TASK_UNALLOCATED.equals(statusKey)) {
				MedicalCall call = new MedicalCall();
				BeanUtils.copyProperties(callUntreated, call);
				this.iBaseDao.deleteObject(callUntreated);
				this.iBaseDao.addObject(call);
			}else if(SystemConfig.CALL_TASK_PENDING.equals(statusKey)) {
				MedicalCallPending callPending = new MedicalCallPending();
				BeanUtils.copyProperties(callUntreated, callPending);
				this.iBaseDao.deleteObject(callUntreated);
				this.iBaseDao.addObject(callPending);				
			}else if(SystemConfig.CALL_TASK_TRANSFER.equals(statusKey)) {
				callUntreated.setTransferUser("");
			}else if(SystemConfig.CALL_TASK_END.equals(statusKey)) {
				MedicalCallEnd callEnd = new MedicalCallEnd();
				BeanUtils.copyProperties(callUntreated, callEnd);
				this.iBaseDao.deleteObject(callUntreated);
				this.iBaseDao.addObject(callEnd);
			}else if(SystemConfig.CALL_TASK_TERMINATE.equals(statusKey)) {
				MedicalCallTerminate callTerminate = new MedicalCallTerminate();
				BeanUtils.copyProperties(callUntreated, callTerminate);
				this.iBaseDao.deleteObject(callUntreated);
				this.iBaseDao.addObject(callTerminate);
			}else {
				break;
			}
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallPendingSta(String callIds, String statusKey) {
		String callId[] = callIds.split(",");
		MedicalCallPending callPending = null;
		for(String cId : callId) {
			callPending = (MedicalCallPending) this.iBaseDao.getObject(MedicalCallPending.class, cId);
			if(SystemConfig.CALL_TASK_UNALLOCATED.equals(statusKey)) {
				MedicalCall call = new MedicalCall();
				BeanUtils.copyProperties(callPending, call);
				this.iBaseDao.deleteObject(callPending);
				this.iBaseDao.addObject(call);
			}else if(SystemConfig.CALL_TASK_UNTREATED.equals(statusKey)) {
				MedicalCallUntreated callUntreated = new MedicalCallUntreated();
				BeanUtils.copyProperties(callPending, callUntreated);
				callUntreated.setTransferUser(SystemConfig.CALL_UNT_NO_TRANSFER);
				this.iBaseDao.deleteObject(callPending);
				this.iBaseDao.addObject(callUntreated);				
			}else if(SystemConfig.CALL_TASK_TRANSFER.equals(statusKey)) {
				MedicalCallUntreated callUntreated = new MedicalCallUntreated();
				BeanUtils.copyProperties(callPending, callUntreated);
				callUntreated.setTransferUser("");
				this.iBaseDao.deleteObject(callPending);
				this.iBaseDao.addObject(callUntreated);			
			}else if(SystemConfig.CALL_TASK_END.equals(statusKey)) {
				MedicalCallEnd callEnd = new MedicalCallEnd();
				BeanUtils.copyProperties(callPending, callEnd);
				this.iBaseDao.deleteObject(callPending);
				this.iBaseDao.addObject(callEnd);
			}else if(SystemConfig.CALL_TASK_TERMINATE.equals(statusKey)) {
				MedicalCallTerminate callTerminate = new MedicalCallTerminate();
				BeanUtils.copyProperties(callPending, callTerminate);
				this.iBaseDao.deleteObject(callPending);
				this.iBaseDao.addObject(callTerminate);
			}else {
				break;
			}
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallTransferSta(String callIds, String statusKey) {
		String callId[] = callIds.split(",");
		MedicalCallUntreated callUntreated = null;
		for(String cId : callId) {
			callUntreated = (MedicalCallUntreated) this.iBaseDao.getObject(MedicalCallUntreated.class, cId);
			if(SystemConfig.CALL_TASK_UNALLOCATED.equals(statusKey)) {
				MedicalCall call = new MedicalCall();
				BeanUtils.copyProperties(callUntreated, call);
				this.iBaseDao.deleteObject(callUntreated);
				this.iBaseDao.addObject(call);
			}else if(SystemConfig.CALL_TASK_UNTREATED.equals(statusKey)) {
				callUntreated.setTransferUser(SystemConfig.CALL_UNT_NO_TRANSFER);
			}else if(SystemConfig.CALL_TASK_PENDING.equals(statusKey)) {
				MedicalCallPending callPending = new MedicalCallPending();
				BeanUtils.copyProperties(callUntreated, callPending);
				this.iBaseDao.deleteObject(callUntreated);
				this.iBaseDao.addObject(callPending);		
			}else if(SystemConfig.CALL_TASK_END.equals(statusKey)) {
				MedicalCallEnd callEnd = new MedicalCallEnd();
				BeanUtils.copyProperties(callUntreated, callEnd);
				this.iBaseDao.deleteObject(callUntreated);
				this.iBaseDao.addObject(callEnd);
			}else if(SystemConfig.CALL_TASK_TERMINATE.equals(statusKey)) {
				MedicalCallTerminate callTerminate = new MedicalCallTerminate();
				BeanUtils.copyProperties(callUntreated, callTerminate);
				this.iBaseDao.deleteObject(callUntreated);
				this.iBaseDao.addObject(callTerminate);
			}else {
				break;
			}
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallEndSta(String callIds, String statusKey) {
		String callId[] = callIds.split(",");
		MedicalCallEnd callEnd = null;
		for(String cId : callId) {
			callEnd = (MedicalCallEnd) this.iBaseDao.getObject(MedicalCallEnd.class, cId);
			if(SystemConfig.CALL_TASK_UNALLOCATED.equals(statusKey)) {
				MedicalCall call = new MedicalCall();
				BeanUtils.copyProperties(callEnd, call);
				this.iBaseDao.deleteObject(callEnd);
				this.iBaseDao.addObject(call);
			}else if(SystemConfig.CALL_TASK_UNTREATED.equals(statusKey)) {
				MedicalCallUntreated callUntreated = new MedicalCallUntreated();
				BeanUtils.copyProperties(callEnd, callUntreated);
				callUntreated.setTransferUser(SystemConfig.CALL_UNT_NO_TRANSFER);
				this.iBaseDao.deleteObject(callEnd);
				this.iBaseDao.addObject(callUntreated);				
			}else if(SystemConfig.CALL_TASK_PENDING.equals(statusKey)) {
				MedicalCallPending callPending = new MedicalCallPending();
				BeanUtils.copyProperties(callEnd, callPending);
				this.iBaseDao.deleteObject(callEnd);
				this.iBaseDao.addObject(callPending);		
			}else if(SystemConfig.CALL_TASK_TRANSFER.equals(statusKey)) {
				MedicalCallUntreated callUntreated = new MedicalCallUntreated();
				BeanUtils.copyProperties(callEnd, callUntreated);
				callUntreated.setTransferUser("");
				this.iBaseDao.deleteObject(callEnd);
				this.iBaseDao.addObject(callUntreated);		
			}else if(SystemConfig.CALL_TASK_TERMINATE.equals(statusKey)) {
				MedicalCallTerminate callTerminate = new MedicalCallTerminate();
				BeanUtils.copyProperties(callEnd, callTerminate);
				this.iBaseDao.deleteObject(callEnd);
				this.iBaseDao.addObject(callTerminate);
			}else {
				break;
			}
		}
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallTerminateSta(String callIds, String statusKey) {
		String callId[] = callIds.split(",");
		MedicalCallTerminate callTerminate = null;
		for(String cId : callId) {
			callTerminate = (MedicalCallTerminate) this.iBaseDao.getObject(MedicalCallTerminate.class, cId);
			if(SystemConfig.CALL_TASK_UNALLOCATED.equals(statusKey)) {
				MedicalCall call = new MedicalCall();
				BeanUtils.copyProperties(callTerminate, call);
				this.iBaseDao.deleteObject(callTerminate);
				this.iBaseDao.addObject(call);
			}else if(SystemConfig.CALL_TASK_UNTREATED.equals(statusKey)) {
				MedicalCallUntreated callUntreated = new MedicalCallUntreated();
				BeanUtils.copyProperties(callTerminate, callUntreated);
				callUntreated.setTransferUser(SystemConfig.CALL_UNT_NO_TRANSFER);
				this.iBaseDao.deleteObject(callTerminate);
				this.iBaseDao.addObject(callUntreated);				
			}else if(SystemConfig.CALL_TASK_PENDING.equals(statusKey)) {
				MedicalCallPending callPending = new MedicalCallPending();
				BeanUtils.copyProperties(callTerminate, callPending);
				this.iBaseDao.deleteObject(callTerminate);
				this.iBaseDao.addObject(callPending);		
			}else if(SystemConfig.CALL_TASK_TRANSFER.equals(statusKey)) {
				MedicalCallUntreated callUntreated = new MedicalCallUntreated();
				BeanUtils.copyProperties(callTerminate, callUntreated);
				callUntreated.setTransferUser("");
				this.iBaseDao.deleteObject(callTerminate);
				this.iBaseDao.addObject(callUntreated);		
			}else if(SystemConfig.CALL_TASK_END.equals(statusKey)) {
				MedicalCallEnd callEnd = new MedicalCallEnd();
				BeanUtils.copyProperties(callTerminate, callEnd);
				this.iBaseDao.deleteObject(callTerminate);
				this.iBaseDao.addObject(callEnd);
			}else {
				break;
			}
		}
	}

/*-----------------------------------话务管理方法-----------------------------------------------------------*/	
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void recoverUnaCall(String callsId) {
		String callId[] = callsId.split(",");
		for(String cId : callId) {
			MedicalCall call = (MedicalCall) this.iBaseDao.getObject(MedicalCall.class, cId);
			call.setCenterId("");
			call.setGroupId("");
			call.setHandleUserId("");
			call.setGrade(SystemConfig.CALL_TASK_GRADE_INIT);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void recoverUntCall(String callsId) {
		String callId[] = callsId.split(",");
		for(String cId : callId) {
			MedicalCallUntreated callUntreated = (MedicalCallUntreated) this.iBaseDao.getObject(MedicalCallUntreated.class, cId);
			callUntreated.setCenterId("");
			callUntreated.setGroupId("");
			callUntreated.setHandleUserId("");
			callUntreated.setGrade(SystemConfig.CALL_TASK_GRADE_INIT);
			MedicalCall call = new MedicalCall();
			BeanUtils.copyProperties(callUntreated, call);
			this.iBaseDao.addObject(call);
			this.iBaseDao.deleteObject(callUntreated);
			
			List<MedicalCallTelInfo> telInfoList = this.iBaseDao.getBySql("from MedicalCallTelInfo a where a.callId=? ", null, call.getId());
			List<MedicalEnterpriseOutside> eOutsideList = this.iBaseDao.getBySql("from MedicalEnterpriseOutside a where a.eid=? ", null, call.getHospitalId());
			for(MedicalCallTelInfo telInfo : telInfoList) {
				for(MedicalEnterpriseOutside eOutSide : eOutsideList) {
					this.iBaseDao.updateHql("delete from MedicalCallExtension a where a.customerNum=? ", telInfo.getPhoneNum() + "," + eOutSide.getOutSideNum());
				}
			}
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void recoverPenCall(String callsId) {
		String callId[] = callsId.split(",");
		for(String cId : callId) {
			MedicalCallPending callPending = (MedicalCallPending) this.iBaseDao.getObject(MedicalCallPending.class, cId);
			callPending.setCenterId("");
			callPending.setGroupId("");
			callPending.setHandleUserId("");
			callPending.setGrade(SystemConfig.CALL_TASK_GRADE_INIT);
			MedicalCall call = new MedicalCall();
			BeanUtils.copyProperties(callPending, call);
			this.iBaseDao.addObject(call);
			this.iBaseDao.deleteObject(callPending);
			
			List<MedicalCallTelInfo> telInfoList = this.iBaseDao.getBySql("from MedicalCallTelInfo a where a.callId=? ", null, call.getId());
			List<MedicalEnterpriseOutside> eOutsideList = this.iBaseDao.getBySql("from MedicalEnterpriseOutside a where a.eid=? ", null, call.getHospitalId());
			for(MedicalCallTelInfo telInfo : telInfoList) {
				for(MedicalEnterpriseOutside eOutSide : eOutsideList) {
					this.iBaseDao.updateHql("delete from MedicalCallExtension a where a.customerNum=? ", telInfo.getPhoneNum() + "," + eOutSide.getOutSideNum());
				}
			}
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void recoverEndCall(String callsId) {
		String callId[] = callsId.split(",");
		for(String cId : callId) {
			MedicalCallEnd callEnd = (MedicalCallEnd) this.iBaseDao.getObject(MedicalCallEnd.class, cId);
			callEnd.setCenterId("");
			callEnd.setGroupId("");
			callEnd.setHandleUserId("");
			callEnd.setGrade(SystemConfig.CALL_TASK_GRADE_INIT);
			MedicalCall call = new MedicalCall();
			BeanUtils.copyProperties(callEnd, call);
			call.setIncomingCount(0);
			this.iBaseDao.addObject(call);
			this.iBaseDao.deleteObject(callEnd);
			
			List<MedicalCallTelInfo> telInfoList = this.iBaseDao.getBySql("from MedicalCallTelInfo a where a.callId=? ", null, call.getId());
			List<MedicalEnterpriseOutside> eOutsideList = this.iBaseDao.getBySql("from MedicalEnterpriseOutside a where a.eid=? ", null, call.getHospitalId());
			for(MedicalCallTelInfo telInfo : telInfoList) {
				for(MedicalEnterpriseOutside eOutSide : eOutsideList) {
					this.iBaseDao.updateHql("delete from MedicalCallExtension a where a.customerNum=? ", telInfo.getPhoneNum() + "," + eOutSide.getOutSideNum());
				}
			}
		}
	}

	/*-----------------------------------导入导出方法-----------------------------------------------------------*/	
	@SuppressWarnings("unchecked")
	@Override
	public Workbook exportUna(MedicalAdmin admin,MedicalCall call) throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCall a where a.hospitalId=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		
		if(SystemConfig.CALL_TASK_LIST_UNALLOCATE.equals(call.getListType())) {
			statusSql.append("and a.grade=? ");
		}else {
			statusSql.append("and a.grade <> ? ");
		}
		objectList.add(SystemConfig.CALL_TASK_GRADE_INIT);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		statusSql.append("order by a.createTime desc ");
		
		int i = 0;
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}else{}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		List<MedicalCall> medicalCallList = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
		//遍历所有未分配任务
		for (MedicalCall medicalCall : medicalCallList) {
			i++;
			Row currentRow = sheet.createRow(i);
			//编号
			if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
				currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
			}else{}
			//话务类型
			if(StringUtils.isNotBlank(medicalCall.getCategory())){
				MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), medicalCall.getCategory());
				currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
			}else{}
			//创建时间
			if(null != medicalCall.getCreateTime()){
				currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//更新时间
			if(null != medicalCall.getUpdateTime()){
				currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//呼入次数
			if(null != medicalCall.getIncomingCount()) {
				currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
			}
			//创建者
			if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
				MedicalAdmin user = (MedicalAdmin)this.iBaseDao
						.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
				currentRow.createCell(5).setCellValue(user.getName());
			}else{}
			//备注
			if(StringUtils.isNotBlank(medicalCall.getRemark())){
				currentRow.createCell(6).setCellValue(medicalCall.getRemark());
			}else{}
			//外线号码
			if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
				currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
			}else{}
			//相关手机号
			if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
				currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
			}else{}
		}
		return workbook;
	}


	@Override
	public Workbook exportCurrentUna(String ids) throws Exception {
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext()
				.getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		//获得本页的未分配任务
		if(StringUtils.isNotBlank(ids)){
			String [] taskIds =  ids.split(",");
			for(int j=0;j<taskIds.length;j++){
				// 获得记录
				MedicalCall medicalCall = (MedicalCall) this.iBaseDao
						.getObject(MedicalCall.class,taskIds[j]);
				Row currentRow = sheet.createRow(j+1);
				//编号
				if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
					currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(medicalCall.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(user.getEnterpriseId(), medicalCall.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				//创建时间
				if(null != medicalCall.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//更新时间
				if(null != medicalCall.getUpdateTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//呼入次数
				if(null != medicalCall.getIncomingCount()) {
					currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
				}
				//创建者
				if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
					MedicalAdmin admin = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
					currentRow.createCell(5).setCellValue(admin.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(medicalCall.getRemark())){
					currentRow.createCell(6).setCellValue(medicalCall.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
					currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
					currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
				}else{}
			}
		}else{}
		return workbook;
	}

	@Override
	public Page  findAllCallByPage(MedicalCall call, Map<String, Object> map) {
		String sql = "SELECT * FROM medical_call_untreated "+
				"UNION SELECT *, NULL AS '1' FROM medical_call "+
				"UNION SELECT *, NULL AS '2' FROM medical_call_pending "+
				"UNION SELECT *, NULL AS '3' FROM medical_call_end "+
				"UNION SELECT *, NULL AS '4' FROM medical_call_terminate ";
		StringBuilder statusSql = new StringBuilder("SELECT * FROM ("+sql+") as a where a.hospital_id=? ");
		StringBuilder countSql = new StringBuilder("select count(*) from ("+sql+") as a where a.hospital_id=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(call.getHospitalId());
		if(null != call.getStime()){
			statusSql.append("and a.create_time >= ? ");
			countSql.append("and a.create_time >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.create_time <= ? ");
			countSql.append("and a.create_time <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serial_num = ? ");
			countSql.append("and a.serial_num = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			countSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.related_phone = ? ");
			countSql.append("and a.related_phone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outside_num = ? ");
			countSql.append("and a.outside_num = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.create_user = ? ");
			countSql.append("and a.create_user = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.update_time >= ? ");
			countSql.append("and a.update_time >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.update_time <= ? ");
			countSql.append("and a.update_time <= ? ");
			objectList.add(call.getUetime());
		}
		
		Page page = this.iBaseDao.getSqlPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20), map, countSql.toString(), statusSql.toString(), objectList);
		List<MedicalCallUntreated> list = new ArrayList<MedicalCallUntreated>();
		for(Object obj : page.getResult()) {
			Object[] object = (Object[]) obj;
			MedicalCallUntreated untreated = (MedicalCallUntreated) object[0];
			if(StringUtils.isNotBlank(untreated.getCreateUser())) {
				MedicalAdmin ma = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, untreated.getCreateUser());
				untreated.setCreateUserName(ma.getName());
			}
			MedicalKeyValue category = this.iBaseDao.getValueByKey(call.getHospitalId(), untreated.getCategory());
			if(null != category){
				untreated.setCategoryValue(category.getKeyValue());
			}
			if(StringUtils.isNotBlank(untreated.getHandleUserId())) {
				MedicalAdmin handleUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, untreated.getHandleUserId());
				if(null != handleUser) {
					untreated.setHandleUserName(handleUser.getName());
				}
			}
			
			MedicalCallCustomer customer = (MedicalCallCustomer) this.iBaseDao.getObjectByHql("from MedicalCallCustomer m where m.callId = ? ", untreated.getId());
			if(null != customer) {
				int totalNum = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? ", customer.getId());
				untreated.setTotalNum(totalNum);
				int singular = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and m.ordersStatus = ? ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
				untreated.setSingular(singular);
				int unpaired = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and (m.ordersStatus <> ? or m.ordersStatus is null) ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
				untreated.setUnpaired(unpaired);
			}
			list.add(untreated);
		}
		
		page.setResult(list);
		return page;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Workbook callManageExportAll(MedicalAdmin admin,MedicalCall call) throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCall a where a.hospitalId=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		
		if(SystemConfig.CALL_TASK_LIST_UNALLOCATE.equals(call.getListType())) {
			statusSql.append("and a.grade=? ");
		}else {
			statusSql.append("and a.grade <> ? ");
		}
		objectList.add(SystemConfig.CALL_TASK_GRADE_INIT);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		statusSql.append("order by a.createTime desc ");
		
		int i = 0;
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}else{}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		List<MedicalCall> medicalCallList = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
		//遍历所有未分配任务
		for (MedicalCall medicalCall : medicalCallList) {
			i++;
			Row currentRow = sheet.createRow(i);
			//编号
			if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
				currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
			}else{}
			//话务类型
			if(StringUtils.isNotBlank(medicalCall.getCategory())){
				MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), medicalCall.getCategory());
				currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
			}else{}
			//创建时间
			if(null != medicalCall.getCreateTime()){
				currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//更新时间
			if(null != medicalCall.getUpdateTime()){
				currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//呼入次数
			if(null != medicalCall.getIncomingCount()) {
				currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
			}
			//创建者
			if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
				MedicalAdmin user = (MedicalAdmin)this.iBaseDao
						.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
				currentRow.createCell(5).setCellValue(user.getName());
			}else{}
			//备注
			if(StringUtils.isNotBlank(medicalCall.getRemark())){
				currentRow.createCell(6).setCellValue(medicalCall.getRemark());
			}else{}
			//外线号码
			if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
				currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
			}else{}
			//相关手机号
			if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
				currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
			}else{}
		}
		return workbook;
	}
	
	@Override
	public Workbook callManageExportCurrent(String ids) throws Exception {
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext()
				.getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		//获得本页的未分配任务
		if(StringUtils.isNotBlank(ids)){
			String [] taskIds =  ids.split(",");
			for(int j=0;j<taskIds.length;j++){
				// 获得记录
				MedicalCall medicalCall = (MedicalCall) this.iBaseDao
						.getObject(MedicalCall.class,taskIds[j]);
				Row currentRow = sheet.createRow(j+1);
				//编号
				if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
					currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(medicalCall.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(user.getEnterpriseId(), medicalCall.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				//创建时间
				if(null != medicalCall.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//更新时间
				if(null != medicalCall.getUpdateTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//呼入次数
				if(null != medicalCall.getIncomingCount()) {
					currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
				}
				//创建者
				if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
					MedicalAdmin admin = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
					currentRow.createCell(5).setCellValue(admin.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(medicalCall.getRemark())){
					currentRow.createCell(6).setCellValue(medicalCall.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
					currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
					currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
				}else{}
			}
		}else{}
		return workbook;
	}
	
	@Override
	public Workbook callManageUntreatedExportCurrent(String ids) throws Exception {
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext()
				.getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		//获得本页的未分配任务
		if(StringUtils.isNotBlank(ids)){
			String [] taskIds =  ids.split(",");
			for(int j=0;j<taskIds.length;j++){
				// 获得记录
				MedicalCallUntreated medicalCall = (MedicalCallUntreated) this.iBaseDao
						.getObject(MedicalCallUntreated.class,taskIds[j]);
				Row currentRow = sheet.createRow(j+1);
				//编号
				if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
					currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(medicalCall.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(user.getEnterpriseId(), medicalCall.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				//创建时间
				if(null != medicalCall.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//更新时间
				if(null != medicalCall.getUpdateTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//呼入次数
				if(null != medicalCall.getIncomingCount()) {
					currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
				}
				//创建者
				if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
					MedicalAdmin admin = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
					currentRow.createCell(5).setCellValue(admin.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(medicalCall.getRemark())){
					currentRow.createCell(6).setCellValue(medicalCall.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
					currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
					currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
				}else{}
			}
		}else{}
		return workbook;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Workbook callManageUntreatedExportAll(MedicalAdmin admin,MedicalCallUntreated call) throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCallUntreated a where a.hospitalId=? and a.grade <> ? and a.transferUser=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		objectList.add(SystemConfig.CALL_TASK_GRADE_INIT);
		objectList.add(SystemConfig.CALL_UNT_NO_TRANSFER);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		statusSql.append("order by a.createTime desc ");
		
		int i = 0;
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}else{}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		List<MedicalCallUntreated> medicalCallList = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
		//遍历所有未分配任务
		for (MedicalCallUntreated medicalCall : medicalCallList) {
			i++;
			Row currentRow = sheet.createRow(i);
			//编号
			if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
				currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
			}else{}
			//话务类型
			if(StringUtils.isNotBlank(medicalCall.getCategory())){
				MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), medicalCall.getCategory());
				currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
			}else{}
			//创建时间
			if(null != medicalCall.getCreateTime()){
				currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//更新时间
			if(null != medicalCall.getUpdateTime()){
				currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//呼入次数
			if(null != medicalCall.getIncomingCount()) {
				currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
			}
			//创建者
			if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
				MedicalAdmin user = (MedicalAdmin)this.iBaseDao
						.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
				currentRow.createCell(5).setCellValue(user.getName());
			}else{}
			//备注
			if(StringUtils.isNotBlank(medicalCall.getRemark())){
				currentRow.createCell(6).setCellValue(medicalCall.getRemark());
			}else{}
			//外线号码
			if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
				currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
			}else{}
			//相关手机号
			if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
				currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
			}else{}
		}
		return workbook;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Workbook callManageTransferExportAll(MedicalAdmin admin,MedicalCallUntreated call) throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCallUntreated a where a.hospitalId=? and a.grade <> ? and a.transferUser <> ? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		objectList.add(SystemConfig.CALL_TASK_GRADE_INIT);
		objectList.add(SystemConfig.CALL_UNT_NO_TRANSFER);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		statusSql.append("order by a.createTime desc ");
		
		int i = 0;
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}else{}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		List<MedicalCallUntreated> medicalCallList = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
		//遍历所有未分配任务
		for (MedicalCallUntreated medicalCall : medicalCallList) {
			i++;
			Row currentRow = sheet.createRow(i);
			//编号
			if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
				currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
			}else{}
			//话务类型
			if(StringUtils.isNotBlank(medicalCall.getCategory())){
				MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), medicalCall.getCategory());
				currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
			}else{}
			//创建时间
			if(null != medicalCall.getCreateTime()){
				currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//更新时间
			if(null != medicalCall.getUpdateTime()){
				currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//呼入次数
			if(null != medicalCall.getIncomingCount()) {
				currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
			}
			//创建者
			if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
				MedicalAdmin user = (MedicalAdmin)this.iBaseDao
						.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
				currentRow.createCell(5).setCellValue(user.getName());
			}else{}
			//备注
			if(StringUtils.isNotBlank(medicalCall.getRemark())){
				currentRow.createCell(6).setCellValue(medicalCall.getRemark());
			}else{}
			//外线号码
			if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
				currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
			}else{}
			//相关手机号
			if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
				currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
			}else{}
		}
		return workbook;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Workbook callManagePendingExportAll(MedicalAdmin admin,
			MedicalCallPending call) throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCallPending a where a.hospitalId=? and a.grade <> ? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		objectList.add(SystemConfig.CALL_TASK_GRADE_INIT);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		statusSql.append("order by a.createTime desc ");
		
		int i = 0;
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}else{}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		List<MedicalCallPending> medicalCallList = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
		//遍历所有未分配任务
		for (MedicalCallPending medicalCall : medicalCallList) {
			i++;
			Row currentRow = sheet.createRow(i);
			//编号
			if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
				currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
			}else{}
			//话务类型
			if(StringUtils.isNotBlank(medicalCall.getCategory())){
				MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), medicalCall.getCategory());
				currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
			}else{}
			//创建时间
			if(null != medicalCall.getCreateTime()){
				currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//更新时间
			if(null != medicalCall.getUpdateTime()){
				currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//呼入次数
			if(null != medicalCall.getIncomingCount()) {
				currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
			}
			//创建者
			if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
				MedicalAdmin user = (MedicalAdmin)this.iBaseDao
						.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
				currentRow.createCell(5).setCellValue(user.getName());
			}else{}
			//备注
			if(StringUtils.isNotBlank(medicalCall.getRemark())){
				currentRow.createCell(6).setCellValue(medicalCall.getRemark());
			}else{}
			//外线号码
			if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
				currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
			}else{}
			//相关手机号
			if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
				currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
			}else{}
		}
		return workbook;
	}

	@Override
	public Workbook callManagePendingExportCurrent(String ids) throws Exception {
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext()
				.getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		//获得本页的未分配任务
		if(StringUtils.isNotBlank(ids)){
			String [] taskIds =  ids.split(",");
			for(int j=0;j<taskIds.length;j++){
				// 获得记录
				MedicalCallPending medicalCall = (MedicalCallPending) this.iBaseDao
						.getObject(MedicalCallPending.class,taskIds[j]);
				Row currentRow = sheet.createRow(j+1);
				//编号
				if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
					currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(medicalCall.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(user.getEnterpriseId(), medicalCall.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				//创建时间
				if(null != medicalCall.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//更新时间
				if(null != medicalCall.getUpdateTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//呼入次数
				if(null != medicalCall.getIncomingCount()) {
					currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
				}
				//创建者
				if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
					MedicalAdmin admin = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
					currentRow.createCell(5).setCellValue(admin.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(medicalCall.getRemark())){
					currentRow.createCell(6).setCellValue(medicalCall.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
					currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
					currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
				}else{}
			}
		}else{}
		return workbook;
	}

	@Override
	public Workbook callManageEndExportCurrent(String ids) throws Exception {
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext()
				.getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		//获得本页的未分配任务
		if(StringUtils.isNotBlank(ids)){
			String [] taskIds =  ids.split(",");
			for(int j=0;j<taskIds.length;j++){
				// 获得记录
				MedicalCallEnd medicalCall = (MedicalCallEnd) this.iBaseDao
						.getObject(MedicalCallEnd.class,taskIds[j]);
				Row currentRow = sheet.createRow(j+1);
				//编号
				if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
					currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(medicalCall.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(user.getEnterpriseId(), medicalCall.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				//创建时间
				if(null != medicalCall.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//更新时间
				if(null != medicalCall.getUpdateTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//呼入次数
				if(null != medicalCall.getIncomingCount()) {
					currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
				}
				//创建者
				if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
					MedicalAdmin admin = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
					currentRow.createCell(5).setCellValue(admin.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(medicalCall.getRemark())){
					currentRow.createCell(6).setCellValue(medicalCall.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
					currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
					currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
				}else{}
			}
		}else{}
		return workbook;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Workbook callManageEndExportAll(MedicalAdmin admin,
			MedicalCallEnd call) throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCallEnd a where a.hospitalId=? and a.grade <> ? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		objectList.add(SystemConfig.CALL_TASK_GRADE_INIT);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		statusSql.append("order by a.createTime desc ");
		
		int i = 0;
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}else{}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		List<MedicalCallEnd> medicalCallList = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
		//遍历所有未分配任务
		for (MedicalCallEnd medicalCall : medicalCallList) {
			i++;
			Row currentRow = sheet.createRow(i);
			//编号
			if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
				currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
			}else{}
			//话务类型
			if(StringUtils.isNotBlank(medicalCall.getCategory())){
				MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), medicalCall.getCategory());
				currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
			}else{}
			//创建时间
			if(null != medicalCall.getCreateTime()){
				currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//更新时间
			if(null != medicalCall.getUpdateTime()){
				currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//呼入次数
			if(null != medicalCall.getIncomingCount()) {
				currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
			}
			//创建者
			if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
				MedicalAdmin user = (MedicalAdmin)this.iBaseDao
						.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
				currentRow.createCell(5).setCellValue(user.getName());
			}else{}
			//备注
			if(StringUtils.isNotBlank(medicalCall.getRemark())){
				currentRow.createCell(6).setCellValue(medicalCall.getRemark());
			}else{}
			//外线号码
			if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
				currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
			}else{}
			//相关手机号
			if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
				currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
			}else{}
		}
		return workbook;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Workbook callManageTerminateExportAll(MedicalAdmin admin,
			MedicalCallTerminate call) throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCallTerminate a where a.hospitalId=? and a.grade <> ? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		objectList.add(SystemConfig.CALL_TASK_GRADE_INIT);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		statusSql.append("order by a.createTime desc ");
		
		int i = 0;
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}else{}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		List<MedicalCallTerminate> medicalCallList = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
		//遍历所有未分配任务
		for (MedicalCallTerminate medicalCall : medicalCallList) {
			i++;
			Row currentRow = sheet.createRow(i);
			//编号
			if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
				currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
			}else{}
			//话务类型
			if(StringUtils.isNotBlank(medicalCall.getCategory())){
				MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), medicalCall.getCategory());
				currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
			}else{}
			//创建时间
			if(null != medicalCall.getCreateTime()){
				currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//更新时间
			if(null != medicalCall.getUpdateTime()){
				currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//呼入次数
			if(null != medicalCall.getIncomingCount()) {
				currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
			}
			//创建者
			if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
				MedicalAdmin user = (MedicalAdmin)this.iBaseDao
						.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
				currentRow.createCell(5).setCellValue(user.getName());
			}else{}
			//备注
			if(StringUtils.isNotBlank(medicalCall.getRemark())){
				currentRow.createCell(6).setCellValue(medicalCall.getRemark());
			}else{}
			//外线号码
			if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
				currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
			}else{}
			//相关手机号
			if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
				currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
			}else{}
		}
		return workbook;
	}

	@Override
	public Workbook callManageTerminateExportCurrent(String ids)
			throws Exception {
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext()
				.getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		//获得本页的未分配任务
		if(StringUtils.isNotBlank(ids)){
			String [] taskIds =  ids.split(",");
			for(int j=0;j<taskIds.length;j++){
				// 获得记录
				MedicalCallTerminate medicalCall = (MedicalCallTerminate) this.iBaseDao
						.getObject(MedicalCallTerminate.class,taskIds[j]);
				Row currentRow = sheet.createRow(j+1);
				//编号
				if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
					currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(medicalCall.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(user.getEnterpriseId(), medicalCall.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				//创建时间
				if(null != medicalCall.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//更新时间
				if(null != medicalCall.getUpdateTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//呼入次数
				if(null != medicalCall.getIncomingCount()) {
					currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
				}
				//创建者
				if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
					MedicalAdmin admin = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
					currentRow.createCell(5).setCellValue(admin.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(medicalCall.getRemark())){
					currentRow.createCell(6).setCellValue(medicalCall.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
					currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
					currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
				}else{}
			}
		}else{}
		return workbook;
	}

	@Override
	public List<Object> incomfindCall(String did, String phoneNum) {
		// 根据外线号码 查企业
		MedicalEnterpriseOutside eDid = (MedicalEnterpriseOutside) this.iBaseDao.getObjectByHql("from MedicalEnterpriseOutside a where a.outSideNum=? ", did);
		List<Object> callList = new ArrayList<Object>();
		if(null != eDid) {
			// 查询电话号码信息是否存在
			MedicalCallTelInfo telInfo = (MedicalCallTelInfo) this.iBaseDao.getObjectByHql("from MedicalCallTelInfo a where a.eid=? and a.phoneNum=? ", eDid.getEid(), phoneNum);
			if(null != telInfo) {
				// 查询未分配任务表
				MedicalCall call = (MedicalCall) this.iBaseDao.getObject(MedicalCall.class, telInfo.getCallId());
				if(null == call) {
					// 查未处理任务表
					MedicalCallUntreated callUnt = (MedicalCallUntreated) this.iBaseDao.getObject(MedicalCallUntreated.class, telInfo.getCallId());
					if(null == callUnt) {
						// 查询待处理任务表
						MedicalCallPending callPen = (MedicalCallPending) this.iBaseDao.getObject(MedicalCallPending.class, telInfo.getCallId());
						if(null == callPen) {
							// 查已结束任务表
							MedicalCallEnd callEnd = (MedicalCallEnd) this.iBaseDao.getObject(MedicalCall.class, telInfo.getCallId());
							if(null == callEnd) {
								// 查询已终止任务
								MedicalCallTerminate callTer = (MedicalCallTerminate) this.iBaseDao.getObject(MedicalCallTerminate.class, telInfo.getCallId());
								if(null != callTer) {
									callList.add(SystemConfig.CALL_TASK_TERMINATE);
									callList.add(callTer.getId());
									callList.add(callTer.getCategory());
									callList.add(callTer.getIncomingCount());
									callList.add(callTer.getCreateTime());
								} 
							} else {
								callList.add(SystemConfig.CALL_TASK_END);
								callList.add(callEnd.getId());
								callList.add(callEnd.getCategory());
								callList.add(callEnd.getIncomingCount());
								callList.add(callEnd.getCreateTime());
							}
						} else {
							callList.add(SystemConfig.CALL_TASK_PENDING);
							callList.add(callPen.getId());
							callList.add(callPen.getCategory());
							callList.add(callPen.getIncomingCount());
							callList.add(callPen.getCreateTime());
						}
					} else {
						callList.add(SystemConfig.CALL_TASK_UNTREATED);
						callList.add(callUnt.getId());
						callList.add(callUnt.getCategory());
						callList.add(callUnt.getIncomingCount());
						callList.add(callUnt.getCreateTime());
					}
				} else {
					callList.add(SystemConfig.CALL_TASK_UNALLOCATED);
					callList.add(call.getId());
					callList.add(call.getCategory());
					callList.add(call.getIncomingCount());
					callList.add(call.getCreateTime());
				}
			}
		} else {
			callList.add(eDid);
		}
		return callList;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public String incomsaveCall(MedicalAdmin admin, MedicalAlternately alternately) {
		MedicalEnterpriseOutside eDid = (MedicalEnterpriseOutside) this.iBaseDao.getObjectByHql("from MedicalEnterpriseOutside a where a.outSideNum=? ", alternately.getDid());
		StringBuilder callIdAndStatus = new StringBuilder("");
		if(null != eDid) {
			MedicalCall call = new MedicalCall();
			String uid = UUID.randomUUID().toString();
			String id = uid.replace("-", "");
			call.setId(id);
			call.setSerialNum(SerialNumUtil.code());
			call.setCategory(SystemConfig.CALL_FIRST_TYPE);
			call.setOutsideNum(alternately.getDid());
			call.setRelatedPhone(alternately.getPhoneNum());
			call.setIncomingCount(1);
			call.setHospitalId(eDid.getEid());
			
			// 查询任务保存设置
			MedicalKeyValue taskSave = (MedicalKeyValue) this.iBaseDao.getObjectByHql("from MedicalKeyValue a where a.hospitalId=? and a.typeName=? ", eDid.getEid(), SystemConfig.TASK_SAVE_TYPE);
			if(SystemConfig.TASK_SAVE_DEPT.equals(taskSave.getKeyName())) {				// 任务保存到话务部未分配表中
				call.setGrade(SystemConfig.CALL_TASK_GRADE_INIT);
				this.iBaseDao.addObject(call);
				callIdAndStatus.append(SystemConfig.CALL_TASK_UNALLOCATED);
			} else if(SystemConfig.TASK_SAVE_USER.equals(taskSave.getKeyName())) {		// 任务保存到话务员未处理表中
				call.setGrade(SystemConfig.CALL_TASK_GRADE_USER);
				call.setHandleUserId(admin.getId());
				if(!admin.getIsSuper().equals(SystemConfig.SUPER_ADMIN)) {
					call.setCenterId(admin.getGroup().getPlatform().getId());
					call.setGroupId(admin.getGroup().getId());
				}
				MedicalCallUntreated callUnt = new MedicalCallUntreated();
				BeanUtils.copyProperties(call, callUnt);
				callUnt.setTransferUser(SystemConfig.CALL_UNT_NO_TRANSFER);
				callUnt.setAllocationTime(new Date());
				this.iBaseDao.addObject(callUnt);
				callIdAndStatus.append(SystemConfig.CALL_TASK_UNTREATED);
				
				
				
				
				
				// 保存二次进线号码
				MedicalCallExtension callExtension = new MedicalCallExtension();
				callExtension.setAttendantNo(admin.getUsername());
				callExtension.setCustomerNum(alternately.getPhoneNum() + "," + alternately.getDid());
				this.iBaseDao.saveObject(callExtension);
				
			} else {																			// 根据热线、散线进行划分保存位置
				List<MedicalMediaPlacement> mPmList = this.iBaseDao.getBySql("from MedicalMediaPlacement a where a.media.eid=? ", null, eDid.getEid());
				boolean flag = false;
				for(MedicalMediaPlacement bean : mPmList) {
					if(SystemConfig.MEDIA_PLACEMENT_PALYING.equals(bean.getPlaying())) {		// 判断是否播放中
						if(!DateUtils.isSameDay(new Date(), bean.getPlaceStartTime()) && !DateUtils.isSameDay(new Date(), bean.getPlaceEndTime())) {  // 开始日期、结束日期都不是当天
							if(new Date().after(bean.getPlaceStartTime()) && new Date().before(bean.getPlaceEndTime()) && 
									DateUtil.getTime(DateFormatUtils.format(new Date(), "HH:mm:ss")).after(DateUtil.getTime(bean.getStartTime()))
									&& DateUtil.getTime(DateFormatUtils.format(new Date(), "HH:mm:ss")).before(DateUtil.getTime(bean.getEndTime()))) {
								flag = true;
							}
						} else {
							if(DateUtil.getTime(DateFormatUtils.format(new Date(), "HH:mm:ss")).after(DateUtil.getTime(bean.getStartTime())) && 	// 开始日期或结束日期是当天，判断时间点是否是广告时间
									DateUtil.getTime(DateFormatUtils.format(new Date(), "HH:mm:ss")).before(DateUtil.getTime(bean.getEndTime()))) {
								flag = true;
							}
						} 
					}
				}
				
				if(flag) {	// 热线
					if(SystemConfig.TASK_SAVE_USER_DEPT.equals(taskSave.getKeyName())) {		// 散线保存到话务员，热线话务部
						call.setGrade(SystemConfig.CALL_TASK_GRADE_INIT);
						this.iBaseDao.addObject(call);
						callIdAndStatus.append(SystemConfig.CALL_TASK_UNALLOCATED);
					} else if(SystemConfig.TASK_SAVE_DEPT_USER.equals(taskSave.getKeyName())) {	// 散线保存到话务部，热线话务员
						call.setGrade(SystemConfig.CALL_TASK_GRADE_USER);
						call.setHandleUserId(admin.getId());
						if(!admin.getIsSuper().equals(SystemConfig.SUPER_ADMIN)) {
							call.setCenterId(admin.getGroup().getPlatform().getId());
							call.setGroupId(admin.getGroup().getId());
						}
						MedicalCallUntreated callUnt = new MedicalCallUntreated();
						BeanUtils.copyProperties(call, callUnt);
						callUnt.setTransferUser(SystemConfig.CALL_UNT_NO_TRANSFER);
						callUnt.setAllocationTime(new Date());
						this.iBaseDao.addObject(callUnt);
						
						// 保存二次进线号码
						MedicalCallExtension callExtension = new MedicalCallExtension();
						callExtension.setAttendantNo(admin.getUsername());
						callExtension.setCustomerNum(alternately.getPhoneNum() + "," + alternately.getDid());
						this.iBaseDao.saveObject(callExtension);
						callIdAndStatus.append(SystemConfig.CALL_TASK_UNTREATED);
					}
				} else {  	// 以下是散线
					if(SystemConfig.TASK_SAVE_USER_DEPT.equals(taskSave.getKeyName())) {		// 散线保存到话务员，热线话务部
						call.setGrade(SystemConfig.CALL_TASK_GRADE_USER);
						call.setHandleUserId(admin.getId());
						if(!admin.getIsSuper().equals(SystemConfig.SUPER_ADMIN)) {
							call.setCenterId(admin.getGroup().getPlatform().getId());
							call.setGroupId(admin.getGroup().getId());
						}
						MedicalCallUntreated callUnt = new MedicalCallUntreated();
						BeanUtils.copyProperties(call, callUnt);
						callUnt.setTransferUser(SystemConfig.CALL_UNT_NO_TRANSFER);
						callUnt.setAllocationTime(new Date());
						this.iBaseDao.addObject(callUnt);
						
						// 保存二次进线号码
						MedicalCallExtension callExtension = new MedicalCallExtension();
						callExtension.setAttendantNo(admin.getUsername());
						callExtension.setCustomerNum(alternately.getPhoneNum() + "," + alternately.getDid());
						this.iBaseDao.saveObject(callExtension);
						callIdAndStatus.append(SystemConfig.CALL_TASK_UNTREATED);
					} else if(SystemConfig.TASK_SAVE_DEPT_USER.equals(taskSave.getKeyName())) {	// 散线保存到话务部，热线话务员
						call.setGrade(SystemConfig.CALL_TASK_GRADE_INIT);
						this.iBaseDao.addObject(call);
						callIdAndStatus.append(SystemConfig.CALL_TASK_UNALLOCATED);
					} 
				}
			}
			
			MedicalCallTelInfo telInfo = new MedicalCallTelInfo();
			telInfo.setPhoneNum(alternately.getPhoneNum());
			
			String validNum = PhoneNumUtil.checkNumber(alternately.getPhoneNum()).toString();
			String type = "";
			if(SystemConfig.CELLPHONE_TYPE.equals(validNum.substring(0, validNum.lastIndexOf(",")))) {
				type = SystemConfig.CUSTOMER_TEL_PHONE_TYPE;
			} else if(SystemConfig.FIXEDPHONE_TYPE.equals(validNum.substring(0, validNum.lastIndexOf(",")))) {
				type = SystemConfig.CUSTOMER_TEL_FIXED_TYPE;
			}
			telInfo.setType(type);
			
			telInfo.setIsMain(SystemConfig.CUSTOMER_TEL_IS_MAIN);
			telInfo.setCallId(call.getId());
			telInfo.setEid(eDid.getEid());
			this.iBaseDao.saveObject(telInfo);
			callIdAndStatus.append("|");
			callIdAndStatus.append(call.getId());
		}
		return callIdAndStatus.toString();
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void busyTurn(String callId, String extension) {
		MedicalCall call = (MedicalCall) this.iBaseDao.getObject(MedicalCall.class, callId);
		MedicalAdmin admin = (MedicalAdmin) this.iBaseDao.getObjectByHql("from MedicalAdmin a where a.phoneNumber=? ", extension);
		if(null != call) {
			call.setAllocationTime(new Date());
			call.setHandleUserId(admin.getId());
			call.setGrade(SystemConfig.CALL_TASK_GRADE_USER);
			MedicalCallUntreated callUnt = new MedicalCallUntreated();
			BeanUtils.copyProperties(call, callUnt);
			this.iBaseDao.addObject(callUnt);
			this.iBaseDao.deleteObject(call);
		} else {
			MedicalCallUntreated callUnt = (MedicalCallUntreated) this.iBaseDao.getObject(MedicalCallUntreated.class, callId);
			if(null != callUnt) {
				callUnt.setHandleUserId(admin.getId());
			} else {
				MedicalCallPending callPen = (MedicalCallPending) this.iBaseDao.getObject(MedicalCallPending.class, callId);
				if(null != callPen) {
					MedicalCallUntreated bean = new MedicalCallUntreated();
					BeanUtils.copyProperties(callPen, bean);
					bean.setHandleUserId(admin.getId());
					bean.setTransferUser(SystemConfig.CALL_UNT_NO_TRANSFER);
					this.iBaseDao.addObject(bean);
					this.iBaseDao.deleteObject(callPen);
				} 
			}
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public String incomUpdateCall(List<Object> list, MedicalAlternately alternately) {
		
		StringBuilder callIdAndStatus = new StringBuilder(list.get(1).toString());
		if(SystemConfig.CALL_TASK_UNALLOCATED.equals(list.get(0)) || SystemConfig.CALL_TASK_UNTREATED.equals(list.get(0)) || 
				SystemConfig.CALL_TASK_PENDING.equals(list.get(0))) {								// 更新未分配、未处理、待处理任务
			
			StringBuilder hql1 = new StringBuilder("update ");
			StringBuilder hql2 = new StringBuilder("update ");
			
			if(SystemConfig.CALL_TASK_UNALLOCATED.equals(list.get(0))) {
				hql1.append("MedicalCall ");
				hql2.append("MedicalCall ");
				callIdAndStatus.append("|" + SystemConfig.CALL_TASK_UNALLOCATED);
			} else if(SystemConfig.CALL_TASK_UNTREATED.equals(list.get(0))) {
				hql1.append("MedicalCallUntreated ");
				hql2.append("MedicalCallUntreated ");
				callIdAndStatus.append("|" + SystemConfig.CALL_TASK_UNTREATED);
			} else {
				hql1.append("MedicalCallPending ");
				hql2.append("MedicalCallPending ");
				callIdAndStatus.append("|" + SystemConfig.CALL_TASK_PENDING);
			}
			hql1.append("a set a.category=?, a.incomingCount=?, a.updateTime=? where a.id=? ");
			hql2.append("a set a.incomingCount=?, a.updateTime=? where a.id=? ");
			
			Integer incomingCount = 0;
			if(SystemConfig.INCOMING_POPUP_TYPE.equals(alternately.getPopupType())) {
				incomingCount = Integer.parseInt(list.get(3).toString());
			} else {
				incomingCount = Integer.parseInt(list.get(3).toString()) + 1;
			}
			
			// 如果是一次进线，15分钟以内再呼入，则不改状态，只记次数。
			if(SystemConfig.CALL_FIRST_TYPE.equals(list.get(2)) || SystemConfig.CALL_SECOND_TYPE.equals(list.get(2))) {	// 一次进线、二次进线更新次数、类别
				// 不是接听弹屏，才更新
				if(!SystemConfig.INCOMING_POPUP_TYPE.equals(alternately.getPopupType())) {
					// 15分钟以外才将类别更新为二次进线
					Date DrainLineTime = DateUtils.addMinutes((Date) list.get(4), SystemConfig.EDIT_FIRST_LINE_TIME);
					if(new Date().after(DrainLineTime)) {	// 15分钟以后
						this.iBaseDao.updateHql(hql1.toString(), SystemConfig.CALL_SECOND_TYPE, incomingCount, new Date(), list.get(1).toString());
					} else {
						this.iBaseDao.updateHql(hql2.toString(), incomingCount, new Date(), list.get(1).toString());
					}
				}
			
			} else if(SystemConfig.CALL_OMISSION_TYPE.equals(list.get(2))) {	// 漏线根据十五分钟内、外判断
				Date DrainLineTime = DateUtils.addMinutes((Date) list.get(4), SystemConfig.EDIT_DRAIN_LINE_TIME);// 漏线加15分钟间隔时间
				if(new Date().after(DrainLineTime)) {							// 15分钟以后呼入，更新为二次进线，并修改次数
					this.iBaseDao.updateHql(hql1.toString(), SystemConfig.CALL_SECOND_TYPE, incomingCount, new Date(), list.get(1).toString());
				} else {
					this.iBaseDao.updateHql(hql1.toString(), SystemConfig.CALL_FIRST_TYPE, incomingCount, new Date(), list.get(1).toString());
				}
				
			} else {		// 废线
				this.iBaseDao.updateHql(hql2.toString(), incomingCount, new Date(), list.get(1).toString());
			}
			
		} else if(SystemConfig.CALL_TASK_END.equals(list.get(0))) {				// 已结束任务
			MedicalCallEnd callEnd = (MedicalCallEnd) this.iBaseDao.getObject(MedicalCallEnd.class, list.get(1).toString());
			MedicalCallUntreated callUnt = new MedicalCallUntreated();
			BeanUtils.copyProperties(callEnd, callUnt);
			
			if(SystemConfig.CALL_FIRST_TYPE.equals(list.get(2))) {				// 一次进线，修改类别
				callUnt.setCategory(SystemConfig.CALL_SECOND_TYPE);
			} 
			callUnt.setIncomingCount(1);
			callUnt.setTransferUser(SystemConfig.CALL_UNT_NO_TRANSFER);
			callUnt.setUpdateTime(new Date());
			this.iBaseDao.addObject(callUnt);
			callIdAndStatus.append("|" + SystemConfig.CALL_TASK_UNTREATED);
			
		} else if(SystemConfig.CALL_TASK_TERMINATE.equals(list.get(0))) {			// 已终止任务
			MedicalCallTerminate callTer = (MedicalCallTerminate) this.iBaseDao.getObject(MedicalCallTerminate.class, list.get(1).toString());
			MedicalCall call = new MedicalCall();
			BeanUtils.copyProperties(callTer, call);
			if(SystemConfig.CALL_FIRST_TYPE.equals(list.get(2))) {
				call.setCategory(SystemConfig.CALL_SECOND_TYPE);
			} 
			call.setIncomingCount(1);
			call.setUpdateTime(new Date());
			this.iBaseDao.addObject(call);
			callIdAndStatus.append("|" + SystemConfig.CALL_TASK_UNALLOCATED);
		} 
		
		return callIdAndStatus.toString();
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void saveCallCaller(String phoneNum, MedicalAdmin admin) {
		MedicalCallCaller callCaller = new MedicalCallCaller();
		callCaller.setCallerNum(phoneNum);
		callCaller.setHospitalId(admin.getEnterpriseId());
		this.iBaseDao.saveObject(callCaller);
	}

	@Override
	public Page findAllCenterCallByPage(MedicalCall call, Map<String, Object> map, MedicalAdmin admin) {
		String sql = "SELECT * FROM medical_call_untreated "+
				"UNION SELECT *, NULL AS '1' FROM medical_call "+
				"UNION SELECT *, NULL AS '2' FROM medical_call_pending "+
				"UNION SELECT *, NULL AS '3' FROM medical_call_end "+
				"UNION SELECT *, NULL AS '4' FROM medical_call_terminate ";
		StringBuilder statusSql = new StringBuilder("SELECT * FROM ("+sql+") as a where a.hospital_id=? ");
		StringBuilder countSql = new StringBuilder("select count(*) from ("+sql+") as a where a.hospital_id=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		
		if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.center_id = ? ");
			countSql.append("and a.center_id = ? ");
			Hibernate.initialize(admin.getGroup());
			objectList.add(admin.getGroup().getPlatform().getId());
		}
		
		if(null != call.getStime()){
			statusSql.append("and a.create_time >= ? ");
			countSql.append("and a.create_time >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.create_time <= ? ");
			countSql.append("and a.create_time <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serial_num = ? ");
			countSql.append("and a.serial_num = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			countSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.related_phone = ? ");
			countSql.append("and a.related_phone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outside_num = ? ");
			countSql.append("and a.outside_num = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.create_user = ? ");
			countSql.append("and a.create_user = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.update_time >= ? ");
			countSql.append("and a.update_time = ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.update_time <= ? ");
			countSql.append("and a.update_time = ? ");
			objectList.add(call.getUetime());
		}
		
		Page page = this.iBaseDao.getSqlPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20), map, countSql.toString(), statusSql.toString(), objectList);
		List<MedicalCallUntreated> list = new ArrayList<MedicalCallUntreated>();
		for(Object obj : page.getResult()) {
			Object[] object = (Object[]) obj;
			MedicalCallUntreated untreated = (MedicalCallUntreated) object[0];
			if(StringUtils.isNotBlank(untreated.getCreateUser())) {
				MedicalAdmin ma = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, untreated.getCreateUser());
				untreated.setCreateUserName(ma.getName());
			}
			MedicalKeyValue category = this.iBaseDao.getValueByKey(admin.getEnterpriseId(), untreated.getCategory());
			if(null != category){
				untreated.setCategoryValue(category.getKeyValue());
			}
			if(StringUtils.isNotBlank(untreated.getHandleUserId())) {
				MedicalAdmin handleUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, untreated.getHandleUserId());
				if(null != handleUser) {
					untreated.setHandleUserName(handleUser.getName());
				}
			}
			MedicalCallCustomer customer = (MedicalCallCustomer) this.iBaseDao.getObjectByHql("from MedicalCallCustomer m where m.callId = ? ", untreated.getId());
			if(null != customer) {
				int totalNum = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? ", customer.getId());
				untreated.setTotalNum(totalNum);
				int singular = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and m.ordersStatus = ? ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
				untreated.setSingular(singular);
				int unpaired = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and (m.ordersStatus <> ? or m.ordersStatus is null) ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
				untreated.setUnpaired(unpaired);
			}
			list.add(untreated);
		}
		
		page.setResult(list);
		return page;
	}

	@Override
	public Page findAllGroupCallByPage(MedicalCallUntreated call, Map<String, Object> map, MedicalAdmin admin) {
		String sql = "SELECT * FROM medical_call_untreated "+
				"UNION SELECT *, NULL AS '1' FROM medical_call "+
				"UNION SELECT *, NULL AS '2' FROM medical_call_pending "+
				"UNION SELECT *, NULL AS '3' FROM medical_call_end "+
				"UNION SELECT *, NULL AS '4' FROM medical_call_terminate ";
		StringBuilder statusSql = new StringBuilder("SELECT * FROM ("+sql+") as a where a.hospital_id=? ");
		StringBuilder countSql = new StringBuilder("select count(*) from ("+sql+") as a where a.hospital_id=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		
		if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.group_id = ? ");
			countSql.append("and a.group_id = ? ");
			Hibernate.initialize(admin.getGroup());
			objectList.add(admin.getGroup().getId());
		}
		
		if(null != call.getStime()){
			statusSql.append("and a.create_time >= ? ");
			countSql.append("and a.create_time >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.create_time <= ? ");
			countSql.append("and a.create_time <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serial_num = ? ");
			countSql.append("and a.serial_num = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			countSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.related_phone = ? ");
			countSql.append("and a.related_phone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outside_num = ? ");
			countSql.append("and a.outside_num = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.create_user = ? ");
			countSql.append("and a.create_user = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.update_time >= ? ");
			countSql.append("and a.update_time = ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.update_time <= ? ");
			countSql.append("and a.update_time = ? ");
			objectList.add(call.getUetime());
		}
		
		Page page = this.iBaseDao.getSqlPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20), map, countSql.toString(), statusSql.toString(), objectList);
		List<MedicalCallUntreated> list = new ArrayList<MedicalCallUntreated>();
		for(Object obj : page.getResult()) {
			Object[] object = (Object[]) obj;
			MedicalCallUntreated untreated = (MedicalCallUntreated) object[0];
			if(StringUtils.isNotBlank(untreated.getCreateUser())) {
				MedicalAdmin ma = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, untreated.getCreateUser());
				untreated.setCreateUserName(ma.getName());
			}
			MedicalKeyValue category = this.iBaseDao.getValueByKey(admin.getEnterpriseId(), untreated.getCategory());
			if(null != category){
				untreated.setCategoryValue(category.getKeyValue());
			}
			if(StringUtils.isNotBlank(untreated.getHandleUserId())) {
				MedicalAdmin handleUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, untreated.getHandleUserId());
				if(null != handleUser) {
					untreated.setHandleUserName(handleUser.getName());
				}
			}
			MedicalCallCustomer customer = (MedicalCallCustomer) this.iBaseDao.getObjectByHql("from MedicalCallCustomer m where m.callId = ? ", untreated.getId());
			if(null != customer) {
				int totalNum = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? ", customer.getId());
				untreated.setTotalNum(totalNum);
				int singular = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and m.ordersStatus = ? ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
				untreated.setSingular(singular);
				int unpaired = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and (m.ordersStatus <> ? or m.ordersStatus is null) ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
				untreated.setUnpaired(unpaired);
			}
			list.add(untreated);
		}
		
		page.setResult(list);
		return page;
	}

	@Override
	public Workbook callManageAllExportCurrent(String ids) throws Exception {
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext()
				.getRealPath(SystemConfig.EXPORT_CALL_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		//获得本页的未分配任务
		if(StringUtils.isNotBlank(ids)){
			String [] taskIds =  ids.split(",");
			workbook = this.setValues(sheet, workbook, taskIds, user);
		}else{}
		return workbook;
	}
	
	/**
	 * @desc 导出到excel
	 * @param sheet
	 * @param workbook
	 * @param taskIds
	 * @param user
	 * @return workbook
	 * @author dangjh
	 * 2015年2月9日 下午5:07:34
	 */
	private Workbook setValues(Sheet sheet, Workbook workbook, String[] taskIds, MedicalAdmin user) {
		for(int j=0;j<taskIds.length;j++){
			// 获得记录
			MedicalCallTerminate medicalCall = (MedicalCallTerminate) this.iBaseDao
					.getObject(MedicalCallTerminate.class,taskIds[j]);
			if(null != medicalCall) {
				Row currentRow = sheet.createRow(j+1);
				//编号
				if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
					currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(medicalCall.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(user.getEnterpriseId(), medicalCall.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				
				MedicalCallCustomer customer = (MedicalCallCustomer) this.iBaseDao.getObjectByHql("from MedicalCallCustomer m where m.callId = ? ", medicalCall.getId());
				if(null != customer) {
					//订单总数
					int totalNum = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? ", customer.getId());
					currentRow.createCell(2).setCellValue(String.valueOf(totalNum));
					//成单数
					int singular = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and m.ordersStatus = ? ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
					currentRow.createCell(3).setCellValue(String.valueOf(singular));
					//未成单数
					int unpaired = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and (m.ordersStatus <> ? or m.ordersStatus is null) ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
					currentRow.createCell(4).setCellValue(String.valueOf(unpaired));
				}
				//创建时间
				if(null != medicalCall.getCreateTime()){
					currentRow.createCell(5).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//更新时间
				if(null != medicalCall.getUpdateTime()){
					currentRow.createCell(6).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//创建者
				if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
					MedicalAdmin admin = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
					currentRow.createCell(7).setCellValue(admin.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(medicalCall.getRemark())){
					currentRow.createCell(8).setCellValue(medicalCall.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
					currentRow.createCell(9).setCellValue(medicalCall.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
					currentRow.createCell(10).setCellValue(medicalCall.getRelatedPhone());
				}else{}
				continue;
			}
			
			MedicalCall call = (MedicalCall) this.iBaseDao
					.getObject(MedicalCall.class,taskIds[j]);
			if(null != call) {
				Row currentRow = sheet.createRow(j+1);
				//编号
				if(StringUtils.isNotBlank(call.getSerialNum())){
					currentRow.createCell(0).setCellValue(call.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(call.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(user.getEnterpriseId(), call.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				MedicalCallCustomer customer = (MedicalCallCustomer) this.iBaseDao.getObjectByHql("from MedicalCallCustomer m where m.callId = ? ", call.getId());
				if(null != customer) {
					//订单总数
					int totalNum = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? ", customer.getId());
					currentRow.createCell(2).setCellValue(String.valueOf(totalNum));
					//成单数
					int singular = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and m.ordersStatus = ? ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
					currentRow.createCell(3).setCellValue(String.valueOf(singular));
					//未成单数
					int unpaired = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and (m.ordersStatus <> ? or m.ordersStatus is null) ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
					currentRow.createCell(4).setCellValue(String.valueOf(unpaired));
				}
				//创建时间
				if(null != call.getCreateTime()){
					currentRow.createCell(5).setCellValue(DateFormatUtils.format(call.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//更新时间
				if(null != call.getUpdateTime()){
					currentRow.createCell(6).setCellValue(DateFormatUtils.format(call.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//创建者
				if(StringUtils.isNotBlank(call.getCreateUser())){
					MedicalAdmin admin = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, call.getCreateUser());
					currentRow.createCell(7).setCellValue(admin.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(call.getRemark())){
					currentRow.createCell(8).setCellValue(call.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(call.getOutsideNum())){
					currentRow.createCell(9).setCellValue(call.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(call.getRelatedPhone())){
					currentRow.createCell(10).setCellValue(call.getRelatedPhone());
				}else{}
			}
			
			MedicalCallEnd callEnd = (MedicalCallEnd) this.iBaseDao
					.getObject(MedicalCallEnd.class,taskIds[j]);
			if(null != callEnd) {
				Row currentRow = sheet.createRow(j+1);
				//编号
				if(StringUtils.isNotBlank(callEnd.getSerialNum())){
					currentRow.createCell(0).setCellValue(callEnd.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(callEnd.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(user.getEnterpriseId(), callEnd.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				MedicalCallCustomer customer = (MedicalCallCustomer) this.iBaseDao.getObjectByHql("from MedicalCallCustomer m where m.callId = ? ", callEnd.getId());
				if(null != customer) {
					//订单总数
					int totalNum = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? ", customer.getId());
					currentRow.createCell(2).setCellValue(String.valueOf(totalNum));
					//成单数
					int singular = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and m.ordersStatus = ? ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
					currentRow.createCell(3).setCellValue(String.valueOf(singular));
					//未成单数
					int unpaired = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and (m.ordersStatus <> ? or m.ordersStatus is null) ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
					currentRow.createCell(4).setCellValue(String.valueOf(unpaired));
				}
				//创建时间
				if(null != callEnd.getCreateTime()){
					currentRow.createCell(5).setCellValue(DateFormatUtils.format(callEnd.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//更新时间
				if(null != callEnd.getUpdateTime()){
					currentRow.createCell(6).setCellValue(DateFormatUtils.format(callEnd.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//创建者
				if(StringUtils.isNotBlank(callEnd.getCreateUser())){
					MedicalAdmin admin = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, callEnd.getCreateUser());
					currentRow.createCell(7).setCellValue(admin.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(callEnd.getRemark())){
					currentRow.createCell(8).setCellValue(callEnd.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(callEnd.getOutsideNum())){
					currentRow.createCell(9).setCellValue(callEnd.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(callEnd.getRelatedPhone())){
					currentRow.createCell(10).setCellValue(callEnd.getRelatedPhone());
				}else{}
			}
			
			MedicalCallPending callPending = (MedicalCallPending) this.iBaseDao
					.getObject(MedicalCallPending.class,taskIds[j]);
			if(null != callPending) {
				Row currentRow = sheet.createRow(j+1);
				//编号
				if(StringUtils.isNotBlank(callPending.getSerialNum())){
					currentRow.createCell(0).setCellValue(callPending.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(callPending.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(user.getEnterpriseId(), callPending.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				MedicalCallCustomer customer = (MedicalCallCustomer) this.iBaseDao.getObjectByHql("from MedicalCallCustomer m where m.callId = ? ", callPending.getId());
				if(null != customer) {
					//订单总数
					int totalNum = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? ", customer.getId());
					currentRow.createCell(2).setCellValue(String.valueOf(totalNum));
					//成单数
					int singular = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and m.ordersStatus = ? ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
					currentRow.createCell(3).setCellValue(String.valueOf(singular));
					//未成单数
					int unpaired = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and (m.ordersStatus <> ? or m.ordersStatus is null) ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
					currentRow.createCell(4).setCellValue(String.valueOf(unpaired));
				}
				//创建时间
				if(null != callPending.getCreateTime()){
					currentRow.createCell(5).setCellValue(DateFormatUtils.format(callPending.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//更新时间
				if(null != callPending.getUpdateTime()){
					currentRow.createCell(6).setCellValue(DateFormatUtils.format(callPending.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//创建者
				if(StringUtils.isNotBlank(callPending.getCreateUser())){
					MedicalAdmin admin = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, callPending.getCreateUser());
					currentRow.createCell(7).setCellValue(admin.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(callPending.getRemark())){
					currentRow.createCell(8).setCellValue(callPending.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(callPending.getOutsideNum())){
					currentRow.createCell(9).setCellValue(callPending.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(callPending.getRelatedPhone())){
					currentRow.createCell(10).setCellValue(callPending.getRelatedPhone());
				}else{}
			}
			
			MedicalCallUntreated untreated = (MedicalCallUntreated) this.iBaseDao
					.getObject(MedicalCallUntreated.class,taskIds[j]);
			if(null != untreated) {
				Row currentRow = sheet.createRow(j+1);
				//编号
				if(StringUtils.isNotBlank(untreated.getSerialNum())){
					currentRow.createCell(0).setCellValue(untreated.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(untreated.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(user.getEnterpriseId(), untreated.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				MedicalCallCustomer customer = (MedicalCallCustomer) this.iBaseDao.getObjectByHql("from MedicalCallCustomer m where m.callId = ? ", untreated.getId());
				if(null != customer) {
					//订单总数
					int totalNum = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? ", customer.getId());
					currentRow.createCell(2).setCellValue(String.valueOf(totalNum));
					//成单数
					int singular = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and m.ordersStatus = ? ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
					currentRow.createCell(3).setCellValue(String.valueOf(singular));
					//未成单数
					int unpaired = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and (m.ordersStatus <> ? or m.ordersStatus is null) ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
					currentRow.createCell(4).setCellValue(String.valueOf(unpaired));
				}
				//创建时间
				if(null != untreated.getCreateTime()){
					currentRow.createCell(5).setCellValue(DateFormatUtils.format(untreated.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//更新时间
				if(null != untreated.getUpdateTime()){
					currentRow.createCell(6).setCellValue(DateFormatUtils.format(untreated.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//创建者
				if(StringUtils.isNotBlank(untreated.getCreateUser())){
					MedicalAdmin admin = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, untreated.getCreateUser());
					currentRow.createCell(7).setCellValue(admin.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(untreated.getRemark())){
					currentRow.createCell(8).setCellValue(untreated.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(untreated.getOutsideNum())){
					currentRow.createCell(9).setCellValue(untreated.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(untreated.getRelatedPhone())){
					currentRow.createCell(10).setCellValue(untreated.getRelatedPhone());
				}else{}
			}
		}
		return workbook;
	}

	@Override
	public Workbook callManageAllExport(MedicalCall call, MedicalAdmin admin) throws Exception {
		String sql = "SELECT * FROM medical_call_untreated "+
				"UNION SELECT *, NULL AS '1' FROM medical_call "+
				"UNION SELECT *, NULL AS '2' FROM medical_call_pending "+
				"UNION SELECT *, NULL AS '3' FROM medical_call_end "+
				"UNION SELECT *, NULL AS '4' FROM medical_call_terminate ";
		StringBuilder statusSql = new StringBuilder("SELECT * FROM ("+sql+") as a where a.hospital_id=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		
		if(null != call.getStime()){
			statusSql.append("and a.create_time >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.create_time <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serial_num = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.related_phone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outside_num = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.create_user = ? ");
			objectList.add(call.getCreateUser());
		}
		
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext()
				.getRealPath(SystemConfig.EXPORT_CALL_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		
		@SuppressWarnings("unchecked")
		List<Object> list = this.iBaseDao.getByNativeSql(statusSql.toString(), null, objectList.toArray());
		int j = 0;
		for(Object obj : list) {
			Object[] object = (Object[]) obj;
			MedicalCallUntreated untreated = (MedicalCallUntreated) object[0];
			if(StringUtils.isNotBlank(untreated.getCreateUser())) {
				MedicalAdmin ma = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, untreated.getCreateUser());
				untreated.setCreateUserName(ma.getName());
			}
			MedicalKeyValue category = this.iBaseDao.getValueByKey(admin.getEnterpriseId(), untreated.getCategory());
			if(null != category){
				untreated.setCategoryValue(category.getKeyValue());
			}
			MedicalCallCustomer customer = (MedicalCallCustomer) this.iBaseDao.getObjectByHql("from MedicalCallCustomer m where m.callId = ? ", untreated.getId());
			if(null != customer) {
				int totalNum = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? ", customer.getId());
				untreated.setTotalNum(totalNum);
				int singular = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and m.ordersStatus = ? ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
				untreated.setSingular(singular);
				int unpaired = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and (m.ordersStatus <> ? or m.ordersStatus is null) ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
				untreated.setUnpaired(unpaired);
			}
			
			if(null != untreated) {
				Row currentRow = sheet.createRow(++j);
				//编号
				if(StringUtils.isNotBlank(untreated.getSerialNum())){
					currentRow.createCell(0).setCellValue(untreated.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(untreated.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), untreated.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				//订单总数
				if(null != untreated.getTotalNum()) {
					currentRow.createCell(2).setCellValue(String.valueOf(untreated.getTotalNum()));
				}
				//成单数
				if(null != untreated.getSingular()) {
					currentRow.createCell(3).setCellValue(String.valueOf(untreated.getSingular()));
				}
				//未成单数
				if(null != untreated.getUnpaired()) {
					currentRow.createCell(4).setCellValue(String.valueOf(untreated.getUnpaired()));
				}
				//创建时间
				if(null != untreated.getCreateTime()){
					currentRow.createCell(5).setCellValue(DateFormatUtils.format(untreated.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//创建者
				if(StringUtils.isNotBlank(untreated.getCreateUser())){
					MedicalAdmin user = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, untreated.getCreateUser());
					currentRow.createCell(6).setCellValue(user.getName());
				}else{}
				//处理者
				if(StringUtils.isNotBlank(untreated.getHandleUserId())){
					MedicalAdmin user1 = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, untreated.getHandleUserId());
					currentRow.createCell(7).setCellValue(user1.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(untreated.getRemark())){
					currentRow.createCell(8).setCellValue(untreated.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(untreated.getOutsideNum())){
					currentRow.createCell(9).setCellValue(untreated.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(untreated.getRelatedPhone())){
					currentRow.createCell(10).setCellValue(untreated.getRelatedPhone());
				}else{}
			}
		}
		
		return workbook;
	}
	
	@Override
	public Workbook callManageCenterExportAll(MedicalCall call, MedicalAdmin admin) throws Exception {
		String sql = "SELECT * FROM medical_call_untreated "+
				"UNION SELECT *, NULL AS '1' FROM medical_call "+
				"UNION SELECT *, NULL AS '2' FROM medical_call_pending "+
				"UNION SELECT *, NULL AS '3' FROM medical_call_end "+
				"UNION SELECT *, NULL AS '4' FROM medical_call_terminate ";
		StringBuilder statusSql = new StringBuilder("SELECT * FROM ("+sql+") as a where a.hospital_id=? ");
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.center_id = ? ");
			Hibernate.initialize(admin.getGroup());
			objectList.add(admin.getGroup().getPlatform().getId());
		}
		
		if(null != call.getStime()){
			statusSql.append("and a.create_time >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.create_time <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serial_num = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.related_phone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outside_num = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.create_user = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.update_time >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.update_time <= ? ");
			objectList.add(call.getUetime());
		}
		
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext()
				.getRealPath(SystemConfig.EXPORT_CALL_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		
		@SuppressWarnings("unchecked")
		List<Object> list = this.iBaseDao.getByNativeSql(statusSql.toString(), null, objectList.toArray());
		int j = 0;
		for(Object obj : list) {
			Object[] object = (Object[]) obj;
			MedicalCallUntreated untreated = (MedicalCallUntreated) object[0];
			if(StringUtils.isNotBlank(untreated.getCreateUser())) {
				MedicalAdmin ma = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, untreated.getCreateUser());
				untreated.setCreateUserName(ma.getName());
			}
			MedicalKeyValue category = this.iBaseDao.getValueByKey(admin.getEnterpriseId(), untreated.getCategory());
			if(null != category){
				untreated.setCategoryValue(category.getKeyValue());
			}
			MedicalCallCustomer customer = (MedicalCallCustomer) this.iBaseDao.getObjectByHql("from MedicalCallCustomer m where m.callId = ? ", untreated.getId());
			if(null != customer) {
				int totalNum = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? ", customer.getId());
				untreated.setTotalNum(totalNum);
				int singular = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and m.ordersStatus = ? ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
				untreated.setSingular(singular);
				int unpaired = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and (m.ordersStatus <> ? or m.ordersStatus is null) ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
				untreated.setUnpaired(unpaired);
			}
			
			if(null != untreated) {
				Row currentRow = sheet.createRow(++j);
				//编号
				if(StringUtils.isNotBlank(untreated.getSerialNum())){
					currentRow.createCell(0).setCellValue(untreated.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(untreated.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), untreated.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				//订单总数
				if(null != untreated.getTotalNum()) {
					currentRow.createCell(2).setCellValue(String.valueOf(untreated.getTotalNum()));
				}
				//成单数
				if(null != untreated.getSingular()) {
					currentRow.createCell(3).setCellValue(String.valueOf(untreated.getSingular()));
				}
				//未成单数
				if(null != untreated.getUnpaired()) {
					currentRow.createCell(4).setCellValue(String.valueOf(untreated.getUnpaired()));
				}
				//创建时间
				if(null != untreated.getCreateTime()){
					currentRow.createCell(5).setCellValue(DateFormatUtils.format(untreated.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//更新时间
				if(null != untreated.getUpdateTime()){
					currentRow.createCell(6).setCellValue(DateFormatUtils.format(untreated.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//创建者
				if(StringUtils.isNotBlank(untreated.getCreateUser())){
					MedicalAdmin user = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, untreated.getCreateUser());
					currentRow.createCell(7).setCellValue(user.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(untreated.getRemark())){
					currentRow.createCell(8).setCellValue(untreated.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(untreated.getOutsideNum())){
					currentRow.createCell(9).setCellValue(untreated.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(untreated.getRelatedPhone())){
					currentRow.createCell(10).setCellValue(untreated.getRelatedPhone());
				}else{}
			}
		}
		
		return workbook;
	}

	@Override
	public Workbook callManageGroupExportAll(MedicalCallUntreated call, MedicalAdmin admin) throws Exception {
		String sql = "SELECT * FROM medical_call_untreated "+
				"UNION SELECT *, NULL AS '1' FROM medical_call "+
				"UNION SELECT *, NULL AS '2' FROM medical_call_pending "+
				"UNION SELECT *, NULL AS '3' FROM medical_call_end "+
				"UNION SELECT *, NULL AS '4' FROM medical_call_terminate ";
		StringBuilder statusSql = new StringBuilder("SELECT * FROM ("+sql+") as a where a.hospital_id=? ");
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.group_id = ? ");
			Hibernate.initialize(admin.getGroup());
			objectList.add(admin.getGroup().getId());
		}
		
		if(null != call.getStime()){
			statusSql.append("and a.create_time >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.create_time <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serial_num = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.related_phone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outside_num = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.create_user = ? ");
			objectList.add(call.getCreateUser());
		}
		
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext()
				.getRealPath(SystemConfig.EXPORT_CALL_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		
		@SuppressWarnings("unchecked")
		List<Object> list = this.iBaseDao.getByNativeSql(statusSql.toString(), null, objectList.toArray());
		int j = 0;
		for(Object obj : list) {
			Object[] object = (Object[]) obj;
			MedicalCallUntreated untreated = (MedicalCallUntreated) object[0];
			if(StringUtils.isNotBlank(untreated.getCreateUser())) {
				MedicalAdmin ma = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, untreated.getCreateUser());
				untreated.setCreateUserName(ma.getName());
			}
			MedicalKeyValue category = this.iBaseDao.getValueByKey(admin.getEnterpriseId(), untreated.getCategory());
			if(null != category){
				untreated.setCategoryValue(category.getKeyValue());
			}
			MedicalCallCustomer customer = (MedicalCallCustomer) this.iBaseDao.getObjectByHql("from MedicalCallCustomer m where m.callId = ? ", untreated.getId());
			if(null != customer) {
				int totalNum = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? ", customer.getId());
				untreated.setTotalNum(totalNum);
				int singular = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and m.ordersStatus = ? ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
				untreated.setSingular(singular);
				int unpaired = this.iBaseDao.getIntCount("select count(*) from MedicalOrdersStorage m where m.customerId = ? and (m.ordersStatus <> ? or m.ordersStatus is null) ", customer.getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE);
				untreated.setUnpaired(unpaired);
			}
			
			if(null != untreated) {
				Row currentRow = sheet.createRow(++j);
				//编号
				if(StringUtils.isNotBlank(untreated.getSerialNum())){
					currentRow.createCell(0).setCellValue(untreated.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(untreated.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), untreated.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				//订单总数
				if(null != untreated.getTotalNum()) {
					currentRow.createCell(2).setCellValue(String.valueOf(untreated.getTotalNum()));
				}
				//成单数
				if(null != untreated.getSingular()) {
					currentRow.createCell(3).setCellValue(String.valueOf(untreated.getSingular()));
				}
				//未成单数
				if(null != untreated.getUnpaired()) {
					currentRow.createCell(4).setCellValue(String.valueOf(untreated.getUnpaired()));
				}
				//创建时间
				if(null != untreated.getCreateTime()){
					currentRow.createCell(5).setCellValue(DateFormatUtils.format(untreated.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//创建者
				if(StringUtils.isNotBlank(untreated.getCreateUser())){
					MedicalAdmin user = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, untreated.getCreateUser());
					currentRow.createCell(6).setCellValue(user.getName());
				}else{}
				//处理者
				if(StringUtils.isNotBlank(untreated.getHandleUserId())){
					MedicalAdmin user1 = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, untreated.getHandleUserId());
					currentRow.createCell(7).setCellValue(user1.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(untreated.getRemark())){
					currentRow.createCell(8).setCellValue(untreated.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(untreated.getOutsideNum())){
					currentRow.createCell(9).setCellValue(untreated.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(untreated.getRelatedPhone())){
					currentRow.createCell(10).setCellValue(untreated.getRelatedPhone());
				}else{}
			}
		}
		
		return workbook;
	}

	@Override
	public Workbook downloadCallTemp(MedicalAdmin admin) throws Exception {
		// Excel模版对象
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.CALL_TASK_IMPORT_FILE);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		
		List<MedicalKeyValue> keyValList = this.iBaseDao.findValueByType(admin.getEnterpriseId(), SystemConfig.CM_SYSTEM_TYPE, SystemConfig.CALL_TASK_TYPE);
		List<String> callTypeList = new ArrayList<String>();
		for(MedicalKeyValue callType : keyValList){
			callTypeList.add(callType.getKeyValue());
		}
		String[] callTypes = callTypeList.toArray(new String[callTypeList.size()]);
		
		// 设置数据有效性加载在哪个单元格上,四个参数分别是：起始行、终止行、起始列、终止列   
		sheet = ExcelUtil.setPullDown(sheet, callTypes, 1, 500, 1, 1);	// 第一列的前501行都设置为选择列表形式.
		return workbook;
	}
}
